blob: e21bc796b95f13eb84271e8551dbf7e355d54024 [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,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000983 MachineBasicBlock *CurBB,
984 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000985 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000986
Dan Gohmanc2277342008-10-17 21:16:08 +0000987 // If the leaf of the tree is a comparison, merge the condition into
988 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +0000989 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000990 // The operands of the cmp have to be in this block. We don't know
991 // how to export them from some other block. If this is the first block
992 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +0000993 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +0000994 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
995 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000996 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +0000997 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000998 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +0000999 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001000 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001001 } else {
1002 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001003 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001004 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001005
1006 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001007 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1008 SwitchCases.push_back(CB);
1009 return;
1010 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001011 }
1012
1013 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001014 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001015 NULL, TBB, FBB, CurBB);
1016 SwitchCases.push_back(CB);
1017}
1018
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001019/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001020void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001021 MachineBasicBlock *TBB,
1022 MachineBasicBlock *FBB,
1023 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001024 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001025 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001026 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001027 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001028 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001029 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1030 BOp->getParent() != CurBB->getBasicBlock() ||
1031 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1032 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001033 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001034 return;
1035 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001036
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037 // Create TmpBB after CurBB.
1038 MachineFunction::iterator BBI = CurBB;
1039 MachineFunction &MF = DAG.getMachineFunction();
1040 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1041 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001042
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001043 if (Opc == Instruction::Or) {
1044 // Codegen X | Y as:
1045 // jmp_if_X TBB
1046 // jmp TmpBB
1047 // TmpBB:
1048 // jmp_if_Y TBB
1049 // jmp FBB
1050 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001052 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001053 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001055 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001056 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001057 } else {
1058 assert(Opc == Instruction::And && "Unknown merge op!");
1059 // Codegen X & Y as:
1060 // jmp_if_X TmpBB
1061 // jmp FBB
1062 // TmpBB:
1063 // jmp_if_Y TBB
1064 // jmp FBB
1065 //
1066 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001069 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001072 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001073 }
1074}
1075
1076/// If the set of cases should be emitted as a series of branches, return true.
1077/// If we should emit this as a bunch of and/or'd together conditions, return
1078/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001079bool
Dan Gohman2048b852009-11-23 18:04:58 +00001080SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001081 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 // If this is two comparisons of the same values or'd or and'd together, they
1084 // will get folded into a single comparison, so don't emit two blocks.
1085 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1086 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1087 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1088 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1089 return false;
1090 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001091
Chris Lattner133ce872010-01-02 00:00:03 +00001092 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1093 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1094 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1095 Cases[0].CC == Cases[1].CC &&
1096 isa<Constant>(Cases[0].CmpRHS) &&
1097 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1098 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1099 return false;
1100 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1101 return false;
1102 }
1103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001104 return true;
1105}
1106
Dan Gohman46510a72010-04-15 01:51:59 +00001107void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001108 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001110 // Update machine-CFG edges.
1111 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1112
1113 // Figure out which block is immediately after the current one.
1114 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001115 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001116 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001117 NextBlock = BBI;
1118
1119 if (I.isUnconditional()) {
1120 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001121 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001124 if (Succ0MBB != NextBlock)
1125 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001127 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001129 return;
1130 }
1131
1132 // If this condition is one of the special cases we handle, do special stuff
1133 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001134 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001135 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1136
1137 // If this is a series of conditions that are or'd or and'd together, emit
1138 // this as a sequence of branches instead of setcc's with and/or operations.
1139 // For example, instead of something like:
1140 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001141 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001142 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001143 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001144 // or C, F
1145 // jnz foo
1146 // Emit:
1147 // cmp A, B
1148 // je foo
1149 // cmp D, E
1150 // jle foo
1151 //
Dan Gohman46510a72010-04-15 01:51:59 +00001152 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001153 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001154 (BOp->getOpcode() == Instruction::And ||
1155 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001156 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1157 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // If the compares in later blocks need to use values not currently
1159 // exported from this block, export them now. This block should always
1160 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001161 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001162
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001163 // Allow some cases to be rejected.
1164 if (ShouldEmitAsBranches(SwitchCases)) {
1165 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1166 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1167 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1168 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001170 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001171 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172 SwitchCases.erase(SwitchCases.begin());
1173 return;
1174 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001175
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 // Okay, we decided not to do this, remove any inserted MBB's and clear
1177 // SwitchCases.
1178 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001179 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001180
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001181 SwitchCases.clear();
1182 }
1183 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001186 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001187 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001189 // Use visitSwitchCase to actually insert the fast branch sequence for this
1190 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001191 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192}
1193
1194/// visitSwitchCase - Emits the necessary code to represent a single node in
1195/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001196void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1197 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 SDValue Cond;
1199 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001200 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001201
1202 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203 if (CB.CmpMHS == NULL) {
1204 // Fold "(X == true)" to X and "(X == false)" to !X to
1205 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001206 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001207 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001208 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001209 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001210 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001211 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001212 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001213 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 } else {
1216 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1217
Anton Korobeynikov23218582008-12-23 22:25:27 +00001218 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1219 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220
1221 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001222 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223
1224 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001226 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001228 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001229 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231 DAG.getConstant(High-Low, VT), ISD::SETULE);
1232 }
1233 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001234
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001235 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001236 SwitchBB->addSuccessor(CB.TrueBB);
1237 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 // Set NextBlock to be the MBB immediately after the current one, if any.
1240 // This is used to avoid emitting unnecessary branches to the next block.
1241 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001242 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001243 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001244 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001245
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246 // If the lhs block is the next block, invert the condition so that we can
1247 // fall through to the lhs instead of the rhs block.
1248 if (CB.TrueBB == NextBlock) {
1249 std::swap(CB.TrueBB, CB.FalseBB);
1250 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001251 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001252 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001253
Dale Johannesenf5d97892009-02-04 01:48:28 +00001254 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001256 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001258 // If the branch was constant folded, fix up the CFG.
1259 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001260 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001261 } else {
1262 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001263 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001264 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001265
Bill Wendling4533cac2010-01-28 21:51:40 +00001266 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001267 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1268 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001270
1271 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001272}
1273
1274/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001275void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001276 // Emit the code for the jump table
1277 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001278 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001279 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1280 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001281 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001282 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1283 MVT::Other, Index.getValue(1),
1284 Table, Index);
1285 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001286}
1287
1288/// visitJumpTableHeader - This function emits necessary code to produce index
1289/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001290void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001291 JumpTableHeader &JTH,
1292 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001293 // Subtract the lowest switch case value from the value being switched on and
1294 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 // difference between smallest and largest cases.
1296 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001297 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001298 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001299 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001300
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001301 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001302 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001303 // can be used as an index into the jump table in a subsequent basic block.
1304 // This value may be smaller or larger than the target's pointer type, and
1305 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001306 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001309 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1310 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311 JT.Reg = JumpTableReg;
1312
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001313 // Emit the range check for the jump table, and branch to the default block
1314 // for the switch statement if the value being switched on exceeds the largest
1315 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001316 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001317 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001318 DAG.getConstant(JTH.Last-JTH.First,VT),
1319 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320
1321 // Set NextBlock to be the MBB immediately after the current one, if any.
1322 // This is used to avoid emitting unnecessary branches to the next block.
1323 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001324 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001325
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001326 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327 NextBlock = BBI;
1328
Dale Johannesen66978ee2009-01-31 02:22:37 +00001329 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001330 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001331 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001332
Bill Wendling4533cac2010-01-28 21:51:40 +00001333 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001334 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1335 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001336
Bill Wendling87710f02009-12-21 23:47:40 +00001337 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001338}
1339
1340/// visitBitTestHeader - This function emits necessary code to produce value
1341/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001342void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1343 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344 // Subtract the minimum value
1345 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001346 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001347 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001348 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001349
1350 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001351 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001352 TLI.getSetCCResultType(Sub.getValueType()),
1353 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001354 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001355
Bill Wendling87710f02009-12-21 23:47:40 +00001356 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1357 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001358
Duncan Sands92abc622009-01-31 15:50:11 +00001359 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001360 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1361 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362
1363 // Set NextBlock to be the MBB immediately after the current one, if any.
1364 // This is used to avoid emitting unnecessary branches to the next block.
1365 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001366 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001367 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001368 NextBlock = BBI;
1369
1370 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1371
Dan Gohman99be8ae2010-04-19 22:41:47 +00001372 SwitchBB->addSuccessor(B.Default);
1373 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374
Dale Johannesen66978ee2009-01-31 02:22:37 +00001375 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001376 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001377 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001378
Bill Wendling4533cac2010-01-28 21:51:40 +00001379 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001380 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1381 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001382
Bill Wendling87710f02009-12-21 23:47:40 +00001383 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384}
1385
1386/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001387void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1388 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001389 BitTestCase &B,
1390 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001391 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001392 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001393 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001394 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001395 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001396 DAG.getConstant(1, TLI.getPointerTy()),
1397 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001398
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001399 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001400 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001401 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001402 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001403 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1404 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001405 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001406 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001407
Dan Gohman99be8ae2010-04-19 22:41:47 +00001408 SwitchBB->addSuccessor(B.TargetBB);
1409 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001410
Dale Johannesen66978ee2009-01-31 02:22:37 +00001411 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001412 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001413 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414
1415 // Set NextBlock to be the MBB immediately after the current one, if any.
1416 // This is used to avoid emitting unnecessary branches to the next block.
1417 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001418 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001419 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420 NextBlock = BBI;
1421
Bill Wendling4533cac2010-01-28 21:51:40 +00001422 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001423 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1424 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001425
Bill Wendling87710f02009-12-21 23:47:40 +00001426 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001427}
1428
Dan Gohman46510a72010-04-15 01:51:59 +00001429void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001430 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1431
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001432 // Retrieve successors.
1433 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1434 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1435
Gabor Greifb67e6b32009-01-15 11:10:44 +00001436 const Value *Callee(I.getCalledValue());
1437 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 visitInlineAsm(&I);
1439 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001440 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441
1442 // If the value of the invoke is used outside of its defining block, make it
1443 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001444 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445
1446 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001447 InvokeMBB->addSuccessor(Return);
1448 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449
1450 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001451 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1452 MVT::Other, getControlRoot(),
1453 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454}
1455
Dan Gohman46510a72010-04-15 01:51:59 +00001456void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457}
1458
1459/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1460/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001461bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1462 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001463 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001464 MachineBasicBlock *Default,
1465 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001466 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001469 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001471 return false;
1472
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 // Get the MachineFunction which holds the current MBB. This is used when
1474 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001475 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001476
1477 // Figure out which block is immediately after the current one.
1478 MachineBasicBlock *NextBlock = 0;
1479 MachineFunction::iterator BBI = CR.CaseBB;
1480
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001481 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 NextBlock = BBI;
1483
1484 // TODO: If any two of the cases has the same destination, and if one value
1485 // is the same as the other, but has one bit unset that the other has set,
1486 // use bit manipulation to do two compares at once. For example:
1487 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 // Rearrange the case blocks so that the last one falls through if possible.
1490 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1491 // The last case block won't fall through into 'NextBlock' if we emit the
1492 // branches in this order. See if rearranging a case value would help.
1493 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1494 if (I->BB == NextBlock) {
1495 std::swap(*I, BackCase);
1496 break;
1497 }
1498 }
1499 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001500
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001501 // Create a CaseBlock record representing a conditional branch to
1502 // the Case's target mbb if the value being switched on SV is equal
1503 // to C.
1504 MachineBasicBlock *CurBlock = CR.CaseBB;
1505 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1506 MachineBasicBlock *FallThrough;
1507 if (I != E-1) {
1508 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1509 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001510
1511 // Put SV in a virtual register to make it available from the new blocks.
1512 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001513 } else {
1514 // If the last case doesn't match, go to the default block.
1515 FallThrough = Default;
1516 }
1517
Dan Gohman46510a72010-04-15 01:51:59 +00001518 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001519 ISD::CondCode CC;
1520 if (I->High == I->Low) {
1521 // This is just small small case range :) containing exactly 1 case
1522 CC = ISD::SETEQ;
1523 LHS = SV; RHS = I->High; MHS = NULL;
1524 } else {
1525 CC = ISD::SETLE;
1526 LHS = I->Low; MHS = SV; RHS = I->High;
1527 }
1528 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530 // If emitting the first comparison, just call visitSwitchCase to emit the
1531 // code into the current block. Otherwise, push the CaseBlock onto the
1532 // vector to be later processed by SDISel, and insert the node's MBB
1533 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001534 if (CurBlock == SwitchBB)
1535 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001536 else
1537 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001538
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001539 CurBlock = FallThrough;
1540 }
1541
1542 return true;
1543}
1544
1545static inline bool areJTsAllowed(const TargetLowering &TLI) {
1546 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1548 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001549}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001550
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001551static APInt ComputeRange(const APInt &First, const APInt &Last) {
1552 APInt LastExt(Last), FirstExt(First);
1553 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1554 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1555 return (LastExt - FirstExt + 1ULL);
1556}
1557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001559bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1560 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001561 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001562 MachineBasicBlock* Default,
1563 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001564 Case& FrontCase = *CR.Range.first;
1565 Case& BackCase = *(CR.Range.second-1);
1566
Chris Lattnere880efe2009-11-07 07:50:34 +00001567 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1568 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569
Chris Lattnere880efe2009-11-07 07:50:34 +00001570 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001571 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1572 I!=E; ++I)
1573 TSize += I->size();
1574
Dan Gohmane0567812010-04-08 23:03:40 +00001575 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001577
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001578 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001579 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 if (Density < 0.4)
1581 return false;
1582
David Greene4b69d992010-01-05 01:24:57 +00001583 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001584 << "First entry: " << First << ". Last entry: " << Last << '\n'
1585 << "Range: " << Range
1586 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001587
1588 // Get the MachineFunction which holds the current MBB. This is used when
1589 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001590 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591
1592 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001594 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595
1596 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1597
1598 // Create a new basic block to hold the code for loading the address
1599 // of the jump table, and jumping to it. Update successor information;
1600 // we will either branch to the default case for the switch, or the jump
1601 // table.
1602 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1603 CurMF->insert(BBI, JumpTableBB);
1604 CR.CaseBB->addSuccessor(Default);
1605 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001606
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607 // Build a vector of destination BBs, corresponding to each target
1608 // of the jump table. If the value of the jump table slot corresponds to
1609 // a case statement, push the case's BB onto the vector, otherwise, push
1610 // the default BB.
1611 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001612 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001613 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001614 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1615 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001616
1617 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001618 DestBBs.push_back(I->BB);
1619 if (TEI==High)
1620 ++I;
1621 } else {
1622 DestBBs.push_back(Default);
1623 }
1624 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001626 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001627 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1628 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629 E = DestBBs.end(); I != E; ++I) {
1630 if (!SuccsHandled[(*I)->getNumber()]) {
1631 SuccsHandled[(*I)->getNumber()] = true;
1632 JumpTableBB->addSuccessor(*I);
1633 }
1634 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001635
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001636 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001637 unsigned JTEncoding = TLI.getJumpTableEncoding();
1638 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001639 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 // Set the jump table information so that we can codegen it as a second
1642 // MachineBasicBlock
1643 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001644 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1645 if (CR.CaseBB == SwitchBB)
1646 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648 JTCases.push_back(JumpTableBlock(JTH, JT));
1649
1650 return true;
1651}
1652
1653/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1654/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001655bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1656 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001657 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001658 MachineBasicBlock *Default,
1659 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660 // Get the MachineFunction which holds the current MBB. This is used when
1661 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001662 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663
1664 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001665 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001666 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001667
1668 Case& FrontCase = *CR.Range.first;
1669 Case& BackCase = *(CR.Range.second-1);
1670 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1671
1672 // Size is the number of Cases represented by this range.
1673 unsigned Size = CR.Range.second - CR.Range.first;
1674
Chris Lattnere880efe2009-11-07 07:50:34 +00001675 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1676 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001677 double FMetric = 0;
1678 CaseItr Pivot = CR.Range.first + Size/2;
1679
1680 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1681 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001682 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001683 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1684 I!=E; ++I)
1685 TSize += I->size();
1686
Chris Lattnere880efe2009-11-07 07:50:34 +00001687 APInt LSize = FrontCase.size();
1688 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001689 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001690 << "First: " << First << ", Last: " << Last <<'\n'
1691 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1693 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001694 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1695 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001696 APInt Range = ComputeRange(LEnd, RBegin);
1697 assert((Range - 2ULL).isNonNegative() &&
1698 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001699 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001700 (LEnd - First + 1ULL).roundToDouble();
1701 double RDensity = (double)RSize.roundToDouble() /
1702 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001703 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001705 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001706 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1707 << "LDensity: " << LDensity
1708 << ", RDensity: " << RDensity << '\n'
1709 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001710 if (FMetric < Metric) {
1711 Pivot = J;
1712 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001713 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 }
1715
1716 LSize += J->size();
1717 RSize -= J->size();
1718 }
1719 if (areJTsAllowed(TLI)) {
1720 // If our case is dense we *really* should handle it earlier!
1721 assert((FMetric > 0) && "Should handle dense range earlier!");
1722 } else {
1723 Pivot = CR.Range.first + Size/2;
1724 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 CaseRange LHSR(CR.Range.first, Pivot);
1727 CaseRange RHSR(Pivot, CR.Range.second);
1728 Constant *C = Pivot->Low;
1729 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001730
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001732 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001733 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001734 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735 // Pivot's Value, then we can branch directly to the LHS's Target,
1736 // rather than creating a leaf node for it.
1737 if ((LHSR.second - LHSR.first) == 1 &&
1738 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001739 cast<ConstantInt>(C)->getValue() ==
1740 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741 TrueBB = LHSR.first->BB;
1742 } else {
1743 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1744 CurMF->insert(BBI, TrueBB);
1745 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001746
1747 // Put SV in a virtual register to make it available from the new blocks.
1748 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001749 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001750
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001751 // Similar to the optimization above, if the Value being switched on is
1752 // known to be less than the Constant CR.LT, and the current Case Value
1753 // is CR.LT - 1, then we can branch directly to the target block for
1754 // the current Case Value, rather than emitting a RHS leaf node for it.
1755 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001756 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1757 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001758 FalseBB = RHSR.first->BB;
1759 } else {
1760 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1761 CurMF->insert(BBI, FalseBB);
1762 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001763
1764 // Put SV in a virtual register to make it available from the new blocks.
1765 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766 }
1767
1768 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001769 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770 // Otherwise, branch to LHS.
1771 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1772
Dan Gohman99be8ae2010-04-19 22:41:47 +00001773 if (CR.CaseBB == SwitchBB)
1774 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001775 else
1776 SwitchCases.push_back(CB);
1777
1778 return true;
1779}
1780
1781/// handleBitTestsSwitchCase - if current case range has few destination and
1782/// range span less, than machine word bitwidth, encode case range into series
1783/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001784bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1785 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001786 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001787 MachineBasicBlock* Default,
1788 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001789 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001790 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791
1792 Case& FrontCase = *CR.Range.first;
1793 Case& BackCase = *(CR.Range.second-1);
1794
1795 // Get the MachineFunction which holds the current MBB. This is used when
1796 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001797 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001798
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001799 // If target does not have legal shift left, do not emit bit tests at all.
1800 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1801 return false;
1802
Anton Korobeynikov23218582008-12-23 22:25:27 +00001803 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001804 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1805 I!=E; ++I) {
1806 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001807 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001809
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810 // Count unique destinations
1811 SmallSet<MachineBasicBlock*, 4> Dests;
1812 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1813 Dests.insert(I->BB);
1814 if (Dests.size() > 3)
1815 // Don't bother the code below, if there are too much unique destinations
1816 return false;
1817 }
David Greene4b69d992010-01-05 01:24:57 +00001818 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001819 << Dests.size() << '\n'
1820 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001822 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001823 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1824 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001825 APInt cmpRange = maxValue - minValue;
1826
David Greene4b69d992010-01-05 01:24:57 +00001827 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001828 << "Low bound: " << minValue << '\n'
1829 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001830
Dan Gohmane0567812010-04-08 23:03:40 +00001831 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001832 (!(Dests.size() == 1 && numCmps >= 3) &&
1833 !(Dests.size() == 2 && numCmps >= 5) &&
1834 !(Dests.size() >= 3 && numCmps >= 6)))
1835 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001836
David Greene4b69d992010-01-05 01:24:57 +00001837 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001838 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1839
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001840 // Optimize the case where all the case values fit in a
1841 // word without having to subtract minValue. In this case,
1842 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001843 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001844 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001845 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001846 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001848
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001849 CaseBitsVector CasesBits;
1850 unsigned i, count = 0;
1851
1852 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1853 MachineBasicBlock* Dest = I->BB;
1854 for (i = 0; i < count; ++i)
1855 if (Dest == CasesBits[i].BB)
1856 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001857
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001858 if (i == count) {
1859 assert((count < 3) && "Too much destinations to test!");
1860 CasesBits.push_back(CaseBits(0, Dest, 0));
1861 count++;
1862 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001863
1864 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1865 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1866
1867 uint64_t lo = (lowValue - lowBound).getZExtValue();
1868 uint64_t hi = (highValue - lowBound).getZExtValue();
1869
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001870 for (uint64_t j = lo; j <= hi; j++) {
1871 CasesBits[i].Mask |= 1ULL << j;
1872 CasesBits[i].Bits++;
1873 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001874
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001875 }
1876 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001878 BitTestInfo BTC;
1879
1880 // Figure out which block is immediately after the current one.
1881 MachineFunction::iterator BBI = CR.CaseBB;
1882 ++BBI;
1883
1884 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1885
David Greene4b69d992010-01-05 01:24:57 +00001886 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001887 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001888 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001889 << ", Bits: " << CasesBits[i].Bits
1890 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891
1892 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1893 CurMF->insert(BBI, CaseBB);
1894 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1895 CaseBB,
1896 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001897
1898 // Put SV in a virtual register to make it available from the new blocks.
1899 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001900 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001901
1902 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001903 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001904 CR.CaseBB, Default, BTC);
1905
Dan Gohman99be8ae2010-04-19 22:41:47 +00001906 if (CR.CaseBB == SwitchBB)
1907 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001908
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001909 BitTestCases.push_back(BTB);
1910
1911 return true;
1912}
1913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001915size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1916 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001917 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918
1919 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001920 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1922 Cases.push_back(Case(SI.getSuccessorValue(i),
1923 SI.getSuccessorValue(i),
1924 SMBB));
1925 }
1926 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1927
1928 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001929 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930 // Must recompute end() each iteration because it may be
1931 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001932 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1933 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1934 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001935 MachineBasicBlock* nextBB = J->BB;
1936 MachineBasicBlock* currentBB = I->BB;
1937
1938 // If the two neighboring cases go to the same destination, merge them
1939 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001940 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 I->High = J->High;
1942 J = Cases.erase(J);
1943 } else {
1944 I = J++;
1945 }
1946 }
1947
1948 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1949 if (I->Low != I->High)
1950 // A range counts double, since it requires two compares.
1951 ++numCmps;
1952 }
1953
1954 return numCmps;
1955}
1956
Dan Gohman46510a72010-04-15 01:51:59 +00001957void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001958 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001960 // Figure out which block is immediately after the current one.
1961 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001962 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1963
1964 // If there is only the default destination, branch to it if it is not the
1965 // next basic block. Otherwise, just fall through.
1966 if (SI.getNumOperands() == 2) {
1967 // Update machine-CFG edges.
1968
1969 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001970 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001971 if (Default != NextBlock)
1972 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1973 MVT::Other, getControlRoot(),
1974 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001975
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 return;
1977 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001979 // If there are any non-default case statements, create a vector of Cases
1980 // representing each one, and sort the vector so that we can efficiently
1981 // create a binary search tree from them.
1982 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001984 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001985 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001986 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987
1988 // Get the Value to be switched on and default basic blocks, which will be
1989 // inserted into CaseBlock records, representing basic blocks in the binary
1990 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00001991 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992
1993 // Push the initial CaseRec onto the worklist
1994 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001995 WorkList.push_back(CaseRec(SwitchMBB,0,0,
1996 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997
1998 while (!WorkList.empty()) {
1999 // Grab a record representing a case range to process off the worklist
2000 CaseRec CR = WorkList.back();
2001 WorkList.pop_back();
2002
Dan Gohman99be8ae2010-04-19 22:41:47 +00002003 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002004 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002005
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002006 // If the range has few cases (two or less) emit a series of specific
2007 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002008 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002009 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002010
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002011 // If the switch has more than 5 blocks, and at least 40% dense, and the
2012 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002014 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2018 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002019 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 }
2021}
2022
Dan Gohman46510a72010-04-15 01:51:59 +00002023void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002024 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2025
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002026 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002027 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002028 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002029 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002030 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002031 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002032 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2033 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002034 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002035
Bill Wendling4533cac2010-01-28 21:51:40 +00002036 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2037 MVT::Other, getControlRoot(),
2038 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002039}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002040
Dan Gohman46510a72010-04-15 01:51:59 +00002041void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002042 // -0.0 - X --> fneg
2043 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002044 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002045 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2046 const VectorType *DestTy = cast<VectorType>(I.getType());
2047 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002048 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002049 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002050 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002051 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002053 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2054 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055 return;
2056 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002057 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002058 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002059
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002060 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002061 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002062 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002063 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2064 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002065 return;
2066 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002067
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002068 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002069}
2070
Dan Gohman46510a72010-04-15 01:51:59 +00002071void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 SDValue Op1 = getValue(I.getOperand(0));
2073 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002074 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2075 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002076}
2077
Dan Gohman46510a72010-04-15 01:51:59 +00002078void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002079 SDValue Op1 = getValue(I.getOperand(0));
2080 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002081 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002082 Op2.getValueType() != TLI.getShiftAmountTy()) {
2083 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002084 EVT PTy = TLI.getPointerTy();
2085 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002086 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002087 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2088 TLI.getShiftAmountTy(), Op2);
2089 // If the operand is larger than the shift count type but the shift
2090 // count type has enough bits to represent any shift value, truncate
2091 // it now. This is a common case and it exposes the truncate to
2092 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002093 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002094 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2095 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2096 TLI.getShiftAmountTy(), Op2);
2097 // Otherwise we'll need to temporarily settle for some other
2098 // convenient type; type legalization will make adjustments as
2099 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002100 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002101 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002102 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002103 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002104 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002105 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002106 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002107
Bill Wendling4533cac2010-01-28 21:51:40 +00002108 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2109 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002110}
2111
Dan Gohman46510a72010-04-15 01:51:59 +00002112void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002113 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002114 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002115 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002116 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002117 predicate = ICmpInst::Predicate(IC->getPredicate());
2118 SDValue Op1 = getValue(I.getOperand(0));
2119 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002120 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002121
Owen Andersone50ed302009-08-10 22:56:29 +00002122 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002123 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002124}
2125
Dan Gohman46510a72010-04-15 01:51:59 +00002126void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002128 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002130 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002131 predicate = FCmpInst::Predicate(FC->getPredicate());
2132 SDValue Op1 = getValue(I.getOperand(0));
2133 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002134 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002135 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002136 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137}
2138
Dan Gohman46510a72010-04-15 01:51:59 +00002139void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002140 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002141 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2142 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002143 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002144
Bill Wendling49fcff82009-12-21 22:30:11 +00002145 SmallVector<SDValue, 4> Values(NumValues);
2146 SDValue Cond = getValue(I.getOperand(0));
2147 SDValue TrueVal = getValue(I.getOperand(1));
2148 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002149
Bill Wendling4533cac2010-01-28 21:51:40 +00002150 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002151 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002152 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2153 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002154 SDValue(TrueVal.getNode(),
2155 TrueVal.getResNo() + i),
2156 SDValue(FalseVal.getNode(),
2157 FalseVal.getResNo() + i));
2158
Bill Wendling4533cac2010-01-28 21:51:40 +00002159 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2160 DAG.getVTList(&ValueVTs[0], NumValues),
2161 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002162}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002163
Dan Gohman46510a72010-04-15 01:51:59 +00002164void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002165 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2166 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002167 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002168 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002169}
2170
Dan Gohman46510a72010-04-15 01:51:59 +00002171void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002172 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2173 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2174 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002175 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002176 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002177}
2178
Dan Gohman46510a72010-04-15 01:51:59 +00002179void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2181 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2182 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002183 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002184 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185}
2186
Dan Gohman46510a72010-04-15 01:51:59 +00002187void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188 // FPTrunc is never a no-op cast, no need to check
2189 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002190 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002191 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2192 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002193}
2194
Dan Gohman46510a72010-04-15 01:51:59 +00002195void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002196 // FPTrunc is never a no-op cast, no need to check
2197 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002198 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002199 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002200}
2201
Dan Gohman46510a72010-04-15 01:51:59 +00002202void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002203 // FPToUI is never a no-op cast, no need to check
2204 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002205 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002206 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207}
2208
Dan Gohman46510a72010-04-15 01:51:59 +00002209void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210 // FPToSI is never a no-op cast, no need to check
2211 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002212 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002213 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002214}
2215
Dan Gohman46510a72010-04-15 01:51:59 +00002216void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002217 // UIToFP is never a no-op cast, no need to check
2218 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002219 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002220 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221}
2222
Dan Gohman46510a72010-04-15 01:51:59 +00002223void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002224 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002226 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002227 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228}
2229
Dan Gohman46510a72010-04-15 01:51:59 +00002230void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231 // What to do depends on the size of the integer and the size of the pointer.
2232 // We can either truncate, zero extend, or no-op, accordingly.
2233 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT SrcVT = N.getValueType();
2235 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002236 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002237}
2238
Dan Gohman46510a72010-04-15 01:51:59 +00002239void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 // What to do depends on the size of the integer and the size of the pointer.
2241 // We can either truncate, zero extend, or no-op, accordingly.
2242 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002243 EVT SrcVT = N.getValueType();
2244 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002245 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002246}
2247
Dan Gohman46510a72010-04-15 01:51:59 +00002248void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002249 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002250 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251
Bill Wendling49fcff82009-12-21 22:30:11 +00002252 // BitCast assures us that source and destination are the same size so this is
2253 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002254 if (DestVT != N.getValueType())
2255 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2256 DestVT, N)); // convert types.
2257 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002258 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002259}
2260
Dan Gohman46510a72010-04-15 01:51:59 +00002261void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002262 SDValue InVec = getValue(I.getOperand(0));
2263 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002265 TLI.getPointerTy(),
2266 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002267 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2268 TLI.getValueType(I.getType()),
2269 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270}
2271
Dan Gohman46510a72010-04-15 01:51:59 +00002272void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002274 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002275 TLI.getPointerTy(),
2276 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002277 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2278 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002279}
2280
Mon P Wangaeb06d22008-11-10 04:46:22 +00002281// Utility for visitShuffleVector - Returns true if the mask is mask starting
2282// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002283static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2284 unsigned MaskNumElts = Mask.size();
2285 for (unsigned i = 0; i != MaskNumElts; ++i)
2286 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002287 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002288 return true;
2289}
2290
Dan Gohman46510a72010-04-15 01:51:59 +00002291void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002292 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002293 SDValue Src1 = getValue(I.getOperand(0));
2294 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295
Nate Begeman9008ca62009-04-27 18:41:29 +00002296 // Convert the ConstantVector mask operand into an array of ints, with -1
2297 // representing undef values.
2298 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002299 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002300 unsigned MaskNumElts = MaskElts.size();
2301 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002302 if (isa<UndefValue>(MaskElts[i]))
2303 Mask.push_back(-1);
2304 else
2305 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2306 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002307
Owen Andersone50ed302009-08-10 22:56:29 +00002308 EVT VT = TLI.getValueType(I.getType());
2309 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002310 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002311
Mon P Wangc7849c22008-11-16 05:06:27 +00002312 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002313 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2314 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002315 return;
2316 }
2317
2318 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002319 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2320 // Mask is longer than the source vectors and is a multiple of the source
2321 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002322 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002323 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2324 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002325 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2326 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002327 return;
2328 }
2329
Mon P Wangc7849c22008-11-16 05:06:27 +00002330 // Pad both vectors with undefs to make them the same length as the mask.
2331 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002332 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2333 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002334 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002335
Nate Begeman9008ca62009-04-27 18:41:29 +00002336 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2337 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002338 MOps1[0] = Src1;
2339 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002340
2341 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2342 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002343 &MOps1[0], NumConcat);
2344 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002345 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002346 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002347
Mon P Wangaeb06d22008-11-10 04:46:22 +00002348 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002349 SmallVector<int, 8> MappedOps;
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];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002352 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002353 MappedOps.push_back(Idx);
2354 else
2355 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002356 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002357
Bill Wendling4533cac2010-01-28 21:51:40 +00002358 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2359 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002360 return;
2361 }
2362
Mon P Wangc7849c22008-11-16 05:06:27 +00002363 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002364 // Analyze the access pattern of the vector to see if we can extract
2365 // two subvectors and do the shuffle. The analysis is done by calculating
2366 // the range of elements the mask access on both vectors.
2367 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2368 int MaxRange[2] = {-1, -1};
2369
Nate Begeman5a5ca152009-04-29 05:20:52 +00002370 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002371 int Idx = Mask[i];
2372 int Input = 0;
2373 if (Idx < 0)
2374 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002375
Nate Begeman5a5ca152009-04-29 05:20:52 +00002376 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002377 Input = 1;
2378 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002379 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002380 if (Idx > MaxRange[Input])
2381 MaxRange[Input] = Idx;
2382 if (Idx < MinRange[Input])
2383 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002384 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002385
Mon P Wangc7849c22008-11-16 05:06:27 +00002386 // Check if the access is smaller than the vector size and can we find
2387 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002388 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2389 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002390 int StartIdx[2]; // StartIdx to extract from
2391 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002392 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002393 RangeUse[Input] = 0; // Unused
2394 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002395 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002396 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002397 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002398 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002399 RangeUse[Input] = 1; // Extract from beginning of the vector
2400 StartIdx[Input] = 0;
2401 } else {
2402 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002403 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002404 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002405 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002406 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002407 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002408 }
2409
Bill Wendling636e2582009-08-21 18:16:06 +00002410 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002411 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002412 return;
2413 }
2414 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2415 // Extract appropriate subvector and generate a vector shuffle
2416 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002417 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002418 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002419 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002420 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002421 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002422 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002423 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002424
Mon P Wangc7849c22008-11-16 05:06:27 +00002425 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002426 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002427 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002428 int Idx = Mask[i];
2429 if (Idx < 0)
2430 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002431 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002432 MappedOps.push_back(Idx - StartIdx[0]);
2433 else
2434 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002435 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002436
Bill Wendling4533cac2010-01-28 21:51:40 +00002437 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2438 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002439 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002440 }
2441 }
2442
Mon P Wangc7849c22008-11-16 05:06:27 +00002443 // We can't use either concat vectors or extract subvectors so fall back to
2444 // replacing the shuffle with extract and build vector.
2445 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002446 EVT EltVT = VT.getVectorElementType();
2447 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002448 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002449 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002450 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002451 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002452 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002453 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002454 SDValue Res;
2455
Nate Begeman5a5ca152009-04-29 05:20:52 +00002456 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002457 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2458 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002459 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002460 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2461 EltVT, Src2,
2462 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2463
2464 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002465 }
2466 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002467
Bill Wendling4533cac2010-01-28 21:51:40 +00002468 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2469 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002470}
2471
Dan Gohman46510a72010-04-15 01:51:59 +00002472void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002473 const Value *Op0 = I.getOperand(0);
2474 const Value *Op1 = I.getOperand(1);
2475 const Type *AggTy = I.getType();
2476 const Type *ValTy = Op1->getType();
2477 bool IntoUndef = isa<UndefValue>(Op0);
2478 bool FromUndef = isa<UndefValue>(Op1);
2479
2480 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2481 I.idx_begin(), I.idx_end());
2482
Owen Andersone50ed302009-08-10 22:56:29 +00002483 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002484 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002485 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2487
2488 unsigned NumAggValues = AggValueVTs.size();
2489 unsigned NumValValues = ValValueVTs.size();
2490 SmallVector<SDValue, 4> Values(NumAggValues);
2491
2492 SDValue Agg = getValue(Op0);
2493 SDValue Val = getValue(Op1);
2494 unsigned i = 0;
2495 // Copy the beginning value(s) from the original aggregate.
2496 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002497 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002498 SDValue(Agg.getNode(), Agg.getResNo() + i);
2499 // Copy values from the inserted value(s).
2500 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002501 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002502 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2503 // Copy remaining value(s) from the original aggregate.
2504 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002505 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002506 SDValue(Agg.getNode(), Agg.getResNo() + i);
2507
Bill Wendling4533cac2010-01-28 21:51:40 +00002508 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2509 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2510 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002511}
2512
Dan Gohman46510a72010-04-15 01:51:59 +00002513void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002514 const Value *Op0 = I.getOperand(0);
2515 const Type *AggTy = Op0->getType();
2516 const Type *ValTy = I.getType();
2517 bool OutOfUndef = isa<UndefValue>(Op0);
2518
2519 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2520 I.idx_begin(), I.idx_end());
2521
Owen Andersone50ed302009-08-10 22:56:29 +00002522 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002523 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2524
2525 unsigned NumValValues = ValValueVTs.size();
2526 SmallVector<SDValue, 4> Values(NumValValues);
2527
2528 SDValue Agg = getValue(Op0);
2529 // Copy out the selected value(s).
2530 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2531 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002532 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002533 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002534 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002535
Bill Wendling4533cac2010-01-28 21:51:40 +00002536 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2537 DAG.getVTList(&ValValueVTs[0], NumValValues),
2538 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539}
2540
Dan Gohman46510a72010-04-15 01:51:59 +00002541void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542 SDValue N = getValue(I.getOperand(0));
2543 const Type *Ty = I.getOperand(0)->getType();
2544
Dan Gohman46510a72010-04-15 01:51:59 +00002545 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002546 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002547 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002548 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2549 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2550 if (Field) {
2551 // N = N + Offset
2552 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002553 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002554 DAG.getIntPtrConstant(Offset));
2555 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002557 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002558 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2559 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2560
2561 // Offset canonically 0 for unions, but type changes
2562 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 } else {
2564 Ty = cast<SequentialType>(Ty)->getElementType();
2565
2566 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002567 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002568 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002569 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002570 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002571 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002572 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002573 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002574 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002575 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2576 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002577 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002578 else
Evan Chengb1032a82009-02-09 20:54:38 +00002579 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002580
Dale Johannesen66978ee2009-01-31 02:22:37 +00002581 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002582 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002583 continue;
2584 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002585
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002586 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002587 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2588 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002589 SDValue IdxN = getValue(Idx);
2590
2591 // If the index is smaller or larger than intptr_t, truncate or extend
2592 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002593 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002594
2595 // If this is a multiply by a power of two, turn it into a shl
2596 // immediately. This is a very common case.
2597 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002598 if (ElementSize.isPowerOf2()) {
2599 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002600 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002601 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002602 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002603 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002604 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002605 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002606 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002607 }
2608 }
2609
Scott Michelfdc40a02009-02-17 22:15:04 +00002610 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002611 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002612 }
2613 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002614
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002615 setValue(&I, N);
2616}
2617
Dan Gohman46510a72010-04-15 01:51:59 +00002618void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002619 // If this is a fixed sized alloca in the entry block of the function,
2620 // allocate it statically on the stack.
2621 if (FuncInfo.StaticAllocaMap.count(&I))
2622 return; // getValue will auto-populate this.
2623
2624 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002625 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002626 unsigned Align =
2627 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2628 I.getAlignment());
2629
2630 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002631
Chris Lattner0b18e592009-03-17 19:36:00 +00002632 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2633 AllocSize,
2634 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002635
Owen Andersone50ed302009-08-10 22:56:29 +00002636 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002637 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002638
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002639 // Handle alignment. If the requested alignment is less than or equal to
2640 // the stack alignment, ignore it. If the size is greater than or equal to
2641 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002642 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002643 if (Align <= StackAlign)
2644 Align = 0;
2645
2646 // Round the size of the allocation up to the stack alignment size
2647 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002648 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002649 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002650 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002652 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002653 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002654 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002655 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2656
2657 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002658 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002659 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002660 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002661 setValue(&I, DSA);
2662 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002664 // Inform the Frame Information that we have just allocated a variable-sized
2665 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002666 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002667}
2668
Dan Gohman46510a72010-04-15 01:51:59 +00002669void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670 const Value *SV = I.getOperand(0);
2671 SDValue Ptr = getValue(SV);
2672
2673 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002674
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002675 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002676 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002677 unsigned Alignment = I.getAlignment();
2678
Owen Andersone50ed302009-08-10 22:56:29 +00002679 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680 SmallVector<uint64_t, 4> Offsets;
2681 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2682 unsigned NumValues = ValueVTs.size();
2683 if (NumValues == 0)
2684 return;
2685
2686 SDValue Root;
2687 bool ConstantMemory = false;
2688 if (I.isVolatile())
2689 // Serialize volatile loads with other side effects.
2690 Root = getRoot();
2691 else if (AA->pointsToConstantMemory(SV)) {
2692 // Do not serialize (non-volatile) loads of constant memory with anything.
2693 Root = DAG.getEntryNode();
2694 ConstantMemory = true;
2695 } else {
2696 // Do not serialize non-volatile loads against each other.
2697 Root = DAG.getRoot();
2698 }
2699
2700 SmallVector<SDValue, 4> Values(NumValues);
2701 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002702 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002703 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002704 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2705 PtrVT, Ptr,
2706 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002707 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002708 A, SV, Offsets[i], isVolatile,
2709 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002710
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 Values[i] = L;
2712 Chains[i] = L.getValue(1);
2713 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002714
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002715 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002716 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002717 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002718 if (isVolatile)
2719 DAG.setRoot(Chain);
2720 else
2721 PendingLoads.push_back(Chain);
2722 }
2723
Bill Wendling4533cac2010-01-28 21:51:40 +00002724 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2725 DAG.getVTList(&ValueVTs[0], NumValues),
2726 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002727}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002728
Dan Gohman46510a72010-04-15 01:51:59 +00002729void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2730 const Value *SrcV = I.getOperand(0);
2731 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732
Owen Andersone50ed302009-08-10 22:56:29 +00002733 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002734 SmallVector<uint64_t, 4> Offsets;
2735 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2736 unsigned NumValues = ValueVTs.size();
2737 if (NumValues == 0)
2738 return;
2739
2740 // Get the lowered operands. Note that we do this after
2741 // checking if NumResults is zero, because with zero results
2742 // the operands won't have values in the map.
2743 SDValue Src = getValue(SrcV);
2744 SDValue Ptr = getValue(PtrV);
2745
2746 SDValue Root = getRoot();
2747 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002748 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002749 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002750 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002751 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002752
2753 for (unsigned i = 0; i != NumValues; ++i) {
2754 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2755 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002756 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002757 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002758 Add, PtrV, Offsets[i], isVolatile,
2759 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002760 }
2761
Bill Wendling4533cac2010-01-28 21:51:40 +00002762 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2763 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764}
2765
2766/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2767/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002768void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002769 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002770 bool HasChain = !I.doesNotAccessMemory();
2771 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2772
2773 // Build the operand list.
2774 SmallVector<SDValue, 8> Ops;
2775 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2776 if (OnlyLoad) {
2777 // We don't need to serialize loads against other loads.
2778 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002779 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002780 Ops.push_back(getRoot());
2781 }
2782 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002783
2784 // Info is set by getTgtMemInstrinsic
2785 TargetLowering::IntrinsicInfo Info;
2786 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2787
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002788 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002789 if (!IsTgtIntrinsic)
2790 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002791
2792 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002793 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002794 SDValue Op = getValue(I.getOperand(i));
2795 assert(TLI.isTypeLegal(Op.getValueType()) &&
2796 "Intrinsic uses a non-legal type?");
2797 Ops.push_back(Op);
2798 }
2799
Owen Andersone50ed302009-08-10 22:56:29 +00002800 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002801 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2802#ifndef NDEBUG
2803 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2804 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2805 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806 }
Bob Wilson8d919552009-07-31 22:41:21 +00002807#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002808
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002809 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002810 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811
Bob Wilson8d919552009-07-31 22:41:21 +00002812 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813
2814 // Create the node.
2815 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002816 if (IsTgtIntrinsic) {
2817 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002818 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002819 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002820 Info.memVT, Info.ptrVal, Info.offset,
2821 Info.align, Info.vol,
2822 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002823 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002824 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002825 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002826 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002827 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002828 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002829 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002830 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002831 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002832 }
2833
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834 if (HasChain) {
2835 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2836 if (OnlyLoad)
2837 PendingLoads.push_back(Chain);
2838 else
2839 DAG.setRoot(Chain);
2840 }
Bill Wendling856ff412009-12-22 00:12:37 +00002841
Benjamin Kramerf0127052010-01-05 13:12:22 +00002842 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002844 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002845 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002846 }
Bill Wendling856ff412009-12-22 00:12:37 +00002847
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 setValue(&I, Result);
2849 }
2850}
2851
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002852/// GetSignificand - Get the significand and build it into a floating-point
2853/// number with exponent of 1:
2854///
2855/// Op = (Op & 0x007fffff) | 0x3f800000;
2856///
2857/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002858static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002859GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002860 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2861 DAG.getConstant(0x007fffff, MVT::i32));
2862 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2863 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002864 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002865}
2866
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002867/// GetExponent - Get the exponent:
2868///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002869/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002870///
2871/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002872static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002873GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002874 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002875 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2876 DAG.getConstant(0x7f800000, MVT::i32));
2877 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002878 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002879 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2880 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002881 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002882}
2883
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002884/// getF32Constant - Get 32-bit floating point constant.
2885static SDValue
2886getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002887 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002888}
2889
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002890/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002891/// visitIntrinsicCall: I is a call instruction
2892/// Op is the associated NodeType for I
2893const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002894SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2895 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002896 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002897 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002898 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002899 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002900 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002901 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002902 getValue(I.getOperand(2)),
2903 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002904 setValue(&I, L);
2905 DAG.setRoot(L.getValue(1));
2906 return 0;
2907}
2908
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002909// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002910const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002911SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002912 SDValue Op1 = getValue(I.getOperand(1));
2913 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002914
Owen Anderson825b72b2009-08-11 20:47:22 +00002915 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002916 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002917 return 0;
2918}
Bill Wendling74c37652008-12-09 22:08:41 +00002919
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002920/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2921/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002922void
Dan Gohman46510a72010-04-15 01:51:59 +00002923SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002924 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002925 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002926
Eric Christopher551754c2010-04-16 23:37:20 +00002927 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002928 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002929 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002930
2931 // Put the exponent in the right bit position for later addition to the
2932 // final result:
2933 //
2934 // #define LOG2OFe 1.4426950f
2935 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002936 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002937 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002938 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002939
2940 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002941 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2942 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002943
2944 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002945 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002946 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002947
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002948 if (LimitFloatPrecision <= 6) {
2949 // For floating-point precision of 6:
2950 //
2951 // TwoToFractionalPartOfX =
2952 // 0.997535578f +
2953 // (0.735607626f + 0.252464424f * x) * x;
2954 //
2955 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002957 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002959 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2961 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002962 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002964
2965 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002966 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002967 TwoToFracPartOfX, IntegerPartOfX);
2968
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002970 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2971 // For floating-point precision of 12:
2972 //
2973 // TwoToFractionalPartOfX =
2974 // 0.999892986f +
2975 // (0.696457318f +
2976 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2977 //
2978 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002980 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002982 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2984 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002985 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002986 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2987 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002988 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00002989 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002990
2991 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002992 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002993 TwoToFracPartOfX, IntegerPartOfX);
2994
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002996 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2997 // For floating-point precision of 18:
2998 //
2999 // TwoToFractionalPartOfX =
3000 // 0.999999982f +
3001 // (0.693148872f +
3002 // (0.240227044f +
3003 // (0.554906021e-1f +
3004 // (0.961591928e-2f +
3005 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3006 //
3007 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003009 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003010 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003011 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3013 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003014 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003015 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3016 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003017 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003018 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3019 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003020 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003021 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3022 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003023 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3025 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003026 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003027 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003028 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003029
3030 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003031 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003032 TwoToFracPartOfX, IntegerPartOfX);
3033
Owen Anderson825b72b2009-08-11 20:47:22 +00003034 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003035 }
3036 } else {
3037 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003038 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003039 getValue(I.getOperand(1)).getValueType(),
3040 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003041 }
3042
Dale Johannesen59e577f2008-09-05 18:38:42 +00003043 setValue(&I, result);
3044}
3045
Bill Wendling39150252008-09-09 20:39:27 +00003046/// visitLog - Lower a log intrinsic. Handles the special sequences for
3047/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003048void
Dan Gohman46510a72010-04-15 01:51:59 +00003049SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003050 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003051 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003052
Eric Christopher551754c2010-04-16 23:37:20 +00003053 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003054 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003055 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003056 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003057
3058 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003059 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003060 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003061 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003062
3063 // Get the significand and build it into a floating-point number with
3064 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003065 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003066
3067 if (LimitFloatPrecision <= 6) {
3068 // For floating-point precision of 6:
3069 //
3070 // LogofMantissa =
3071 // -1.1609546f +
3072 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003073 //
Bill Wendling39150252008-09-09 20:39:27 +00003074 // error 0.0034276066, which is better than 8 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, 0xbe74c456));
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, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3080 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003081 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003082
Scott Michelfdc40a02009-02-17 22:15:04 +00003083 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003084 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003085 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3086 // For floating-point precision of 12:
3087 //
3088 // LogOfMantissa =
3089 // -1.7417939f +
3090 // (2.8212026f +
3091 // (-1.4699568f +
3092 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3093 //
3094 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003096 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003097 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003098 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3100 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003101 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3103 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003104 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3106 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003107 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003108
Scott Michelfdc40a02009-02-17 22:15:04 +00003109 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003111 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3112 // For floating-point precision of 18:
3113 //
3114 // LogOfMantissa =
3115 // -2.1072184f +
3116 // (4.2372794f +
3117 // (-3.7029485f +
3118 // (2.2781945f +
3119 // (-0.87823314f +
3120 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3121 //
3122 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003123 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003124 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003126 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3128 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003129 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3131 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003132 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3134 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003135 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3137 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003138 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3140 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003141 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003142
Scott Michelfdc40a02009-02-17 22:15:04 +00003143 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003145 }
3146 } else {
3147 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003148 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003149 getValue(I.getOperand(1)).getValueType(),
3150 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003151 }
3152
Dale Johannesen59e577f2008-09-05 18:38:42 +00003153 setValue(&I, result);
3154}
3155
Bill Wendling3eb59402008-09-09 00:28:24 +00003156/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3157/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003158void
Dan Gohman46510a72010-04-15 01:51:59 +00003159SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003160 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003161 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003162
Eric Christopher551754c2010-04-16 23:37:20 +00003163 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003164 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003165 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003166 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003167
Bill Wendling39150252008-09-09 20:39:27 +00003168 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003169 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003170
Bill Wendling3eb59402008-09-09 00:28:24 +00003171 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003172 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003173 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003174
Bill Wendling3eb59402008-09-09 00:28:24 +00003175 // Different possible minimax approximations of significand in
3176 // floating-point for various degrees of accuracy over [1,2].
3177 if (LimitFloatPrecision <= 6) {
3178 // For floating-point precision of 6:
3179 //
3180 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3181 //
3182 // error 0.0049451742, which is more than 7 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, 0xbeb08fe0));
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, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3188 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003189 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003190
Scott Michelfdc40a02009-02-17 22:15:04 +00003191 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003193 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3194 // For floating-point precision of 12:
3195 //
3196 // Log2ofMantissa =
3197 // -2.51285454f +
3198 // (4.07009056f +
3199 // (-2.12067489f +
3200 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003201 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003202 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003204 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003205 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003206 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3208 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003209 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003210 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3211 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003212 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003213 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3214 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003215 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003216
Scott Michelfdc40a02009-02-17 22:15:04 +00003217 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003219 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3220 // For floating-point precision of 18:
3221 //
3222 // Log2ofMantissa =
3223 // -3.0400495f +
3224 // (6.1129976f +
3225 // (-5.3420409f +
3226 // (3.2865683f +
3227 // (-1.2669343f +
3228 // (0.27515199f -
3229 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3230 //
3231 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003233 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003235 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003236 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3237 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003238 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003239 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3240 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003241 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3243 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003244 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3246 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003247 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3249 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003251
Scott Michelfdc40a02009-02-17 22:15:04 +00003252 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003254 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003255 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003256 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003257 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003258 getValue(I.getOperand(1)).getValueType(),
3259 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003260 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003261
Dale Johannesen59e577f2008-09-05 18:38:42 +00003262 setValue(&I, result);
3263}
3264
Bill Wendling3eb59402008-09-09 00:28:24 +00003265/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3266/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003267void
Dan Gohman46510a72010-04-15 01:51:59 +00003268SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003269 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003270 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003271
Eric Christopher551754c2010-04-16 23:37:20 +00003272 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003273 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003274 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003276
Bill Wendling39150252008-09-09 20:39:27 +00003277 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003278 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003280 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003281
3282 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003283 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003284 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003285
3286 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003287 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003288 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003289 // Log10ofMantissa =
3290 // -0.50419619f +
3291 // (0.60948995f - 0.10380950f * x) * x;
3292 //
3293 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003297 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3299 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003300 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003301
Scott Michelfdc40a02009-02-17 22:15:04 +00003302 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003304 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3305 // For floating-point precision of 12:
3306 //
3307 // Log10ofMantissa =
3308 // -0.64831180f +
3309 // (0.91751397f +
3310 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3311 //
3312 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003314 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003316 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3318 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003319 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003320 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3321 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003322 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003323
Scott Michelfdc40a02009-02-17 22:15:04 +00003324 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003326 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003327 // For floating-point precision of 18:
3328 //
3329 // Log10ofMantissa =
3330 // -0.84299375f +
3331 // (1.5327582f +
3332 // (-1.0688956f +
3333 // (0.49102474f +
3334 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3335 //
3336 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003340 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3342 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003343 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3345 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003346 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3348 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3351 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003353
Scott Michelfdc40a02009-02-17 22:15:04 +00003354 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003356 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003357 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003358 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003359 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003360 getValue(I.getOperand(1)).getValueType(),
3361 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003362 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003363
Dale Johannesen59e577f2008-09-05 18:38:42 +00003364 setValue(&I, result);
3365}
3366
Bill Wendlinge10c8142008-09-09 22:39:21 +00003367/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3368/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003369void
Dan Gohman46510a72010-04-15 01:51:59 +00003370SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003371 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003372 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003373
Eric Christopher551754c2010-04-16 23:37:20 +00003374 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003375 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003376 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003377
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003379
3380 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003381 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3382 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003383
3384 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003386 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003387
3388 if (LimitFloatPrecision <= 6) {
3389 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003390 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003391 // TwoToFractionalPartOfX =
3392 // 0.997535578f +
3393 // (0.735607626f + 0.252464424f * x) * x;
3394 //
3395 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003397 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003398 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003399 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3401 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003404 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003406
Scott Michelfdc40a02009-02-17 22:15:04 +00003407 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003409 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3410 // For floating-point precision of 12:
3411 //
3412 // TwoToFractionalPartOfX =
3413 // 0.999892986f +
3414 // (0.696457318f +
3415 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3416 //
3417 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003418 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003419 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003421 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3423 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3426 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003427 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003429 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003430 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003431
Scott Michelfdc40a02009-02-17 22:15:04 +00003432 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003433 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003434 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3435 // For floating-point precision of 18:
3436 //
3437 // TwoToFractionalPartOfX =
3438 // 0.999999982f +
3439 // (0.693148872f +
3440 // (0.240227044f +
3441 // (0.554906021e-1f +
3442 // (0.961591928e-2f +
3443 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3444 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003446 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003447 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003448 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3450 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003451 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3453 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003454 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3456 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003457 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3459 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3462 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003463 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003465 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003467
Scott Michelfdc40a02009-02-17 22:15:04 +00003468 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003470 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003471 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003472 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003473 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003474 getValue(I.getOperand(1)).getValueType(),
3475 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003476 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003477
Dale Johannesen601d3c02008-09-05 01:48:15 +00003478 setValue(&I, result);
3479}
3480
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003481/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3482/// limited-precision mode with x == 10.0f.
3483void
Dan Gohman46510a72010-04-15 01:51:59 +00003484SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003485 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003486 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003487 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003488 bool IsExp10 = false;
3489
Owen Anderson825b72b2009-08-11 20:47:22 +00003490 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003491 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003492 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3493 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3494 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3495 APFloat Ten(10.0f);
3496 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3497 }
3498 }
3499 }
3500
3501 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003502 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003503
3504 // Put the exponent in the right bit position for later addition to the
3505 // final result:
3506 //
3507 // #define LOG2OF10 3.3219281f
3508 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003509 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003510 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003512
3513 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3515 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003516
3517 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003518 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003519 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003520
3521 if (LimitFloatPrecision <= 6) {
3522 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003523 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003524 // twoToFractionalPartOfX =
3525 // 0.997535578f +
3526 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003527 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003528 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003530 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3534 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003535 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003537 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003539
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003540 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003542 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3543 // For floating-point precision of 12:
3544 //
3545 // TwoToFractionalPartOfX =
3546 // 0.999892986f +
3547 // (0.696457318f +
3548 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3549 //
3550 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003552 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3556 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003557 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3559 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003562 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003564
Scott Michelfdc40a02009-02-17 22:15:04 +00003565 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003567 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3568 // For floating-point precision of 18:
3569 //
3570 // TwoToFractionalPartOfX =
3571 // 0.999999982f +
3572 // (0.693148872f +
3573 // (0.240227044f +
3574 // (0.554906021e-1f +
3575 // (0.961591928e-2f +
3576 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3577 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003579 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003581 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003582 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3583 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003584 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3586 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003587 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3589 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003590 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3592 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003593 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3595 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003596 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003598 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003600
Scott Michelfdc40a02009-02-17 22:15:04 +00003601 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003603 }
3604 } else {
3605 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003606 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003607 getValue(I.getOperand(1)).getValueType(),
3608 getValue(I.getOperand(1)),
3609 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003610 }
3611
3612 setValue(&I, result);
3613}
3614
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003615
3616/// ExpandPowI - Expand a llvm.powi intrinsic.
3617static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3618 SelectionDAG &DAG) {
3619 // If RHS is a constant, we can expand this out to a multiplication tree,
3620 // otherwise we end up lowering to a call to __powidf2 (for example). When
3621 // optimizing for size, we only want to do this if the expansion would produce
3622 // a small number of multiplies, otherwise we do the full expansion.
3623 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3624 // Get the exponent as a positive value.
3625 unsigned Val = RHSC->getSExtValue();
3626 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003627
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003628 // powi(x, 0) -> 1.0
3629 if (Val == 0)
3630 return DAG.getConstantFP(1.0, LHS.getValueType());
3631
Dan Gohmanae541aa2010-04-15 04:33:49 +00003632 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003633 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3634 // If optimizing for size, don't insert too many multiplies. This
3635 // inserts up to 5 multiplies.
3636 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3637 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003638 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003639 // powi(x,15) generates one more multiply than it should), but this has
3640 // the benefit of being both really simple and much better than a libcall.
3641 SDValue Res; // Logically starts equal to 1.0
3642 SDValue CurSquare = LHS;
3643 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003644 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003645 if (Res.getNode())
3646 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3647 else
3648 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003649 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003650
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003651 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3652 CurSquare, CurSquare);
3653 Val >>= 1;
3654 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003655
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003656 // If the original was negative, invert the result, producing 1/(x*x*x).
3657 if (RHSC->getSExtValue() < 0)
3658 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3659 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3660 return Res;
3661 }
3662 }
3663
3664 // Otherwise, expand to a libcall.
3665 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3666}
3667
3668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003669/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3670/// we want to emit this as a call to a named external function, return the name
3671/// otherwise lower it and return null.
3672const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003673SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003674 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003675 SDValue Res;
3676
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003677 switch (Intrinsic) {
3678 default:
3679 // By default, turn this into a target intrinsic node.
3680 visitTargetIntrinsic(I, Intrinsic);
3681 return 0;
3682 case Intrinsic::vastart: visitVAStart(I); return 0;
3683 case Intrinsic::vaend: visitVAEnd(I); return 0;
3684 case Intrinsic::vacopy: visitVACopy(I); return 0;
3685 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003686 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003687 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003688 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003689 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003690 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003691 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003692 return 0;
3693 case Intrinsic::setjmp:
3694 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003695 case Intrinsic::longjmp:
3696 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003697 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003698 // Assert for address < 256 since we support only user defined address
3699 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003700 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003701 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003702 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003703 < 256 &&
3704 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003705 SDValue Op1 = getValue(I.getOperand(1));
3706 SDValue Op2 = getValue(I.getOperand(2));
3707 SDValue Op3 = getValue(I.getOperand(3));
3708 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3709 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003710 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003711 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003712 return 0;
3713 }
Chris Lattner824b9582008-11-21 16:42:48 +00003714 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003715 // Assert for address < 256 since we support only user defined address
3716 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003717 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003718 < 256 &&
3719 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003720 SDValue Op1 = getValue(I.getOperand(1));
3721 SDValue Op2 = getValue(I.getOperand(2));
3722 SDValue Op3 = getValue(I.getOperand(3));
3723 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3724 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003725 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003726 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003727 return 0;
3728 }
Chris Lattner824b9582008-11-21 16:42:48 +00003729 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003730 // Assert for address < 256 since we support only user defined address
3731 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003732 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003733 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003734 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003735 < 256 &&
3736 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003737 SDValue Op1 = getValue(I.getOperand(1));
3738 SDValue Op2 = getValue(I.getOperand(2));
3739 SDValue Op3 = getValue(I.getOperand(3));
3740 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3741 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003742
3743 // If the source and destination are known to not be aliases, we can
3744 // lower memmove as memcpy.
3745 uint64_t Size = -1ULL;
3746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003747 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003748 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003749 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003750 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003751 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003752 return 0;
3753 }
3754
Mon P Wang20adc9d2010-04-04 03:10:48 +00003755 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003756 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003757 return 0;
3758 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003759 case Intrinsic::dbg_declare: {
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003760 // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None.
3761 // The real handling of this intrinsic is in FastISel.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003762 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00003763 // FIXME: Variable debug info is not supported here.
3764 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003765 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003766 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003767 return 0;
3768
Devang Patelac1ceb32009-10-09 22:42:28 +00003769 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00003770 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003771 if (!Address)
3772 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003773 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003774 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003775 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel24f20e02009-08-22 17:12:53 +00003776 // Don't handle byval struct arguments or VLAs, for example.
3777 if (!AI)
3778 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003779 DenseMap<const AllocaInst*, int>::iterator SI =
3780 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003781 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00003782 return 0; // VLAs.
3783 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003784
Chris Lattner512063d2010-04-05 06:19:28 +00003785 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3786 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3787 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003788 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003789 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003790 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003791 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003792 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3793 return 0;
3794
3795 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003796 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003797 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003798 if (!V)
3799 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003800
3801 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3802 // but do not always have a corresponding SDNode built. The SDNodeOrder
3803 // absolute, but not relative, values are different depending on whether
3804 // debug info exists.
3805 ++SDNodeOrder;
3806 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Evan Cheng31441b72010-03-29 20:48:30 +00003807 DAG.AddDbgValue(DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003808 } else {
3809 SDValue &N = NodeMap[V];
Evan Cheng31441b72010-03-29 20:48:30 +00003810 if (N.getNode())
3811 DAG.AddDbgValue(DAG.getDbgValue(Variable, N.getNode(),
3812 N.getResNo(), Offset, dl, SDNodeOrder),
3813 N.getNode());
3814 else
Devang Patel00190342010-03-15 19:15:44 +00003815 // We may expand this to cover more cases. One case where we have no
3816 // data available is an unreferenced parameter; we need this fallback.
Evan Cheng31441b72010-03-29 20:48:30 +00003817 DAG.AddDbgValue(DAG.getDbgValue(Variable,
Devang Patel00190342010-03-15 19:15:44 +00003818 UndefValue::get(V->getType()),
Evan Cheng31441b72010-03-29 20:48:30 +00003819 Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003820 }
3821
3822 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003823 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003824 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003825 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003826 // Don't handle byval struct arguments or VLAs, for example.
3827 if (!AI)
3828 return 0;
3829 DenseMap<const AllocaInst*, int>::iterator SI =
3830 FuncInfo.StaticAllocaMap.find(AI);
3831 if (SI == FuncInfo.StaticAllocaMap.end())
3832 return 0; // VLAs.
3833 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003834
Chris Lattner512063d2010-04-05 06:19:28 +00003835 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3836 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3837 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003838 return 0;
3839 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003840 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003841 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003842 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3843 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003844 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003845 SDValue Ops[1];
3846 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003847 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003848 setValue(&I, Op);
3849 DAG.setRoot(Op.getValue(1));
3850 return 0;
3851 }
3852
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003853 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003854 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003855 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003856 if (CallMBB->isLandingPad())
3857 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003858 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003859#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003860 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003861#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003862 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3863 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003864 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003865 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003866
Chris Lattner3a5815f2009-09-17 23:54:54 +00003867 // Insert the EHSELECTION instruction.
3868 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3869 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003870 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003871 Ops[1] = getRoot();
3872 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003873 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003874 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003875 return 0;
3876 }
3877
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003878 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003879 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003880 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003881 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3882 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003883 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003884 return 0;
3885 }
3886
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003887 case Intrinsic::eh_return_i32:
3888 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003889 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3890 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3891 MVT::Other,
3892 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003893 getValue(I.getOperand(1)),
3894 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003895 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003896 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003897 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003898 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003899 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003900 EVT VT = getValue(I.getOperand(1)).getValueType();
3901 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003902 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003903 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003904 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003905 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003906 TLI.getPointerTy()),
3907 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003908 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003909 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003910 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00003911 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
3912 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003913 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003914 }
Jim Grosbachca752c92010-01-28 01:45:32 +00003915 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00003916 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00003917 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00003918 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00003919 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00003920
Chris Lattner512063d2010-04-05 06:19:28 +00003921 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00003922 return 0;
3923 }
3924
Mon P Wang77cdf302008-11-10 20:54:11 +00003925 case Intrinsic::convertff:
3926 case Intrinsic::convertfsi:
3927 case Intrinsic::convertfui:
3928 case Intrinsic::convertsif:
3929 case Intrinsic::convertuif:
3930 case Intrinsic::convertss:
3931 case Intrinsic::convertsu:
3932 case Intrinsic::convertus:
3933 case Intrinsic::convertuu: {
3934 ISD::CvtCode Code = ISD::CVT_INVALID;
3935 switch (Intrinsic) {
3936 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3937 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3938 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3939 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3940 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3941 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3942 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3943 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3944 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3945 }
Owen Andersone50ed302009-08-10 22:56:29 +00003946 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00003947 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003948 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
3949 DAG.getValueType(DestVT),
3950 DAG.getValueType(getValue(Op1).getValueType()),
3951 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00003952 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003953 Code);
3954 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00003955 return 0;
3956 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003957 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00003958 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003959 getValue(I.getOperand(1)).getValueType(),
3960 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003961 return 0;
3962 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00003963 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3964 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003965 return 0;
3966 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00003967 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003968 getValue(I.getOperand(1)).getValueType(),
3969 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003970 return 0;
3971 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00003972 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003973 getValue(I.getOperand(1)).getValueType(),
3974 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003975 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003976 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003977 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003978 return 0;
3979 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003980 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003981 return 0;
3982 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003983 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003984 return 0;
3985 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003986 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003987 return 0;
3988 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003989 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003990 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003991 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003992 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003993 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00003994 case Intrinsic::convert_to_fp16:
3995 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003996 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00003997 return 0;
3998 case Intrinsic::convert_from_fp16:
3999 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004000 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004001 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004002 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004003 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004004 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004005 return 0;
4006 }
4007 case Intrinsic::readcyclecounter: {
4008 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004009 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4010 DAG.getVTList(MVT::i64, MVT::Other),
4011 &Op, 1);
4012 setValue(&I, Res);
4013 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004014 return 0;
4015 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004016 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004017 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004018 getValue(I.getOperand(1)).getValueType(),
4019 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004020 return 0;
4021 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004022 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004023 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004024 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004025 return 0;
4026 }
4027 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004028 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004029 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004030 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004031 return 0;
4032 }
4033 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004034 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004035 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004036 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004037 return 0;
4038 }
4039 case Intrinsic::stacksave: {
4040 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004041 Res = DAG.getNode(ISD::STACKSAVE, dl,
4042 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4043 setValue(&I, Res);
4044 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004045 return 0;
4046 }
4047 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004048 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004049 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004050 return 0;
4051 }
Bill Wendling57344502008-11-18 11:01:33 +00004052 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004053 // Emit code into the DAG to store the stack guard onto the stack.
4054 MachineFunction &MF = DAG.getMachineFunction();
4055 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004056 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004057
Eric Christopher551754c2010-04-16 23:37:20 +00004058 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4059 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004060
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004061 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004062 MFI->setStackProtectorIndex(FI);
4063
4064 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4065
4066 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004067 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4068 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004069 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004070 setValue(&I, Res);
4071 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004072 return 0;
4073 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004074 case Intrinsic::objectsize: {
4075 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004076 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004077
4078 assert(CI && "Non-constant type in __builtin_object_size?");
4079
Eric Christopher551754c2010-04-16 23:37:20 +00004080 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004081 EVT Ty = Arg.getValueType();
4082
Eric Christopherd060b252009-12-23 02:51:48 +00004083 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004084 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004085 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004086 Res = DAG.getConstant(0, Ty);
4087
4088 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004089 return 0;
4090 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004091 case Intrinsic::var_annotation:
4092 // Discard annotate attributes
4093 return 0;
4094
4095 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004096 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004097
4098 SDValue Ops[6];
4099 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004100 Ops[1] = getValue(I.getOperand(1));
4101 Ops[2] = getValue(I.getOperand(2));
4102 Ops[3] = getValue(I.getOperand(3));
4103 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004104 Ops[5] = DAG.getSrcValue(F);
4105
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004106 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4107 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4108 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004109
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004110 setValue(&I, Res);
4111 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004112 return 0;
4113 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004114 case Intrinsic::gcroot:
4115 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004116 const Value *Alloca = I.getOperand(1);
4117 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004119 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4120 GFI->addStackRoot(FI->getIndex(), TypeMap);
4121 }
4122 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004123 case Intrinsic::gcread:
4124 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004125 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004126 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004127 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004128 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004129 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004130 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004131 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004132 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004133 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004134 return implVisitAluOverflow(I, ISD::UADDO);
4135 case Intrinsic::sadd_with_overflow:
4136 return implVisitAluOverflow(I, ISD::SADDO);
4137 case Intrinsic::usub_with_overflow:
4138 return implVisitAluOverflow(I, ISD::USUBO);
4139 case Intrinsic::ssub_with_overflow:
4140 return implVisitAluOverflow(I, ISD::SSUBO);
4141 case Intrinsic::umul_with_overflow:
4142 return implVisitAluOverflow(I, ISD::UMULO);
4143 case Intrinsic::smul_with_overflow:
4144 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004146 case Intrinsic::prefetch: {
4147 SDValue Ops[4];
4148 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004149 Ops[1] = getValue(I.getOperand(1));
4150 Ops[2] = getValue(I.getOperand(2));
4151 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004152 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004153 return 0;
4154 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004156 case Intrinsic::memory_barrier: {
4157 SDValue Ops[6];
4158 Ops[0] = getRoot();
4159 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004160 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004161
Bill Wendling4533cac2010-01-28 21:51:40 +00004162 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004163 return 0;
4164 }
4165 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004166 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004167 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004168 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004169 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004170 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004171 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004172 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004173 getValue(I.getOperand(3)),
4174 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004175 setValue(&I, L);
4176 DAG.setRoot(L.getValue(1));
4177 return 0;
4178 }
4179 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004180 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004181 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004182 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004183 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004184 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004185 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004186 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004187 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004188 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004189 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004190 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004191 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004192 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004193 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004194 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004196 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004197 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004198 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004199 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004200 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004201
4202 case Intrinsic::invariant_start:
4203 case Intrinsic::lifetime_start:
4204 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004205 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004206 return 0;
4207 case Intrinsic::invariant_end:
4208 case Intrinsic::lifetime_end:
4209 // Discard region information.
4210 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004211 }
4212}
4213
Dan Gohman46510a72010-04-15 01:51:59 +00004214void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004215 bool isTailCall,
4216 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004217 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4218 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004219 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004220 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004221 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004222
4223 TargetLowering::ArgListTy Args;
4224 TargetLowering::ArgListEntry Entry;
4225 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004226
4227 // Check whether the function can return without sret-demotion.
4228 SmallVector<EVT, 4> OutVTs;
4229 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4230 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004231 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004232 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004233
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004234 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004235 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4236
4237 SDValue DemoteStackSlot;
4238
4239 if (!CanLowerReturn) {
4240 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4241 FTy->getReturnType());
4242 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4243 FTy->getReturnType());
4244 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004245 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004246 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4247
4248 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4249 Entry.Node = DemoteStackSlot;
4250 Entry.Ty = StackSlotPtrType;
4251 Entry.isSExt = false;
4252 Entry.isZExt = false;
4253 Entry.isInReg = false;
4254 Entry.isSRet = true;
4255 Entry.isNest = false;
4256 Entry.isByVal = false;
4257 Entry.Alignment = Align;
4258 Args.push_back(Entry);
4259 RetTy = Type::getVoidTy(FTy->getContext());
4260 }
4261
Dan Gohman46510a72010-04-15 01:51:59 +00004262 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004263 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004264 SDValue ArgNode = getValue(*i);
4265 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4266
4267 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004268 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4269 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4270 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4271 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4272 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4273 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004274 Entry.Alignment = CS.getParamAlignment(attrInd);
4275 Args.push_back(Entry);
4276 }
4277
Chris Lattner512063d2010-04-05 06:19:28 +00004278 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004279 // Insert a label before the invoke call to mark the try range. This can be
4280 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004281 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004282
Jim Grosbachca752c92010-01-28 01:45:32 +00004283 // For SjLj, keep track of which landing pads go with which invokes
4284 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004285 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004286 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004287 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004288 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004289 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004290 }
4291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004292 // Both PendingLoads and PendingExports must be flushed here;
4293 // this call might not return.
4294 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004295 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 }
4297
Dan Gohman98ca4f22009-08-05 01:29:28 +00004298 // Check if target-independent constraints permit a tail call here.
4299 // Target-dependent constraints are checked within TLI.LowerCallTo.
4300 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004301 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004302 isTailCall = false;
4303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004304 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004305 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004306 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004307 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004308 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004309 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004310 isTailCall,
4311 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004312 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004313 assert((isTailCall || Result.second.getNode()) &&
4314 "Non-null chain expected with non-tail call!");
4315 assert((Result.second.getNode() || !Result.first.getNode()) &&
4316 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004317 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004318 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004319 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004320 // The instruction result is the result of loading from the
4321 // hidden sret parameter.
4322 SmallVector<EVT, 1> PVTs;
4323 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4324
4325 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4326 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4327 EVT PtrVT = PVTs[0];
4328 unsigned NumValues = OutVTs.size();
4329 SmallVector<SDValue, 4> Values(NumValues);
4330 SmallVector<SDValue, 4> Chains(NumValues);
4331
4332 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004333 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4334 DemoteStackSlot,
4335 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004336 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004337 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004338 Values[i] = L;
4339 Chains[i] = L.getValue(1);
4340 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004341
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004342 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4343 MVT::Other, &Chains[0], NumValues);
4344 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004345
4346 // Collect the legal value parts into potentially illegal values
4347 // that correspond to the original function's return values.
4348 SmallVector<EVT, 4> RetTys;
4349 RetTy = FTy->getReturnType();
4350 ComputeValueVTs(TLI, RetTy, RetTys);
4351 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4352 SmallVector<SDValue, 4> ReturnValues;
4353 unsigned CurReg = 0;
4354 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4355 EVT VT = RetTys[I];
4356 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4357 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4358
4359 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004360 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004361 RegisterVT, VT, AssertOp);
4362 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004363 CurReg += NumRegs;
4364 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004365
Bill Wendling4533cac2010-01-28 21:51:40 +00004366 setValue(CS.getInstruction(),
4367 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4368 DAG.getVTList(&RetTys[0], RetTys.size()),
4369 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004370
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004371 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004372
4373 // As a special case, a null chain means that a tail call has been emitted and
4374 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004375 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004376 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004377 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004378 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004379
Chris Lattner512063d2010-04-05 06:19:28 +00004380 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004381 // Insert a label at the end of the invoke call to mark the try range. This
4382 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004383 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004384 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004385
4386 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004387 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004388 }
4389}
4390
Chris Lattner8047d9a2009-12-24 00:37:38 +00004391/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4392/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004393static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4394 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004395 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004396 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004397 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004398 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004399 if (C->isNullValue())
4400 continue;
4401 // Unknown instruction.
4402 return false;
4403 }
4404 return true;
4405}
4406
Dan Gohman46510a72010-04-15 01:51:59 +00004407static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4408 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004409 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004410
Chris Lattner8047d9a2009-12-24 00:37:38 +00004411 // Check to see if this load can be trivially constant folded, e.g. if the
4412 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004413 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004414 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004415 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004416 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004417
Dan Gohman46510a72010-04-15 01:51:59 +00004418 if (const Constant *LoadCst =
4419 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4420 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004421 return Builder.getValue(LoadCst);
4422 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004423
Chris Lattner8047d9a2009-12-24 00:37:38 +00004424 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4425 // still constant memory, the input chain can be the entry node.
4426 SDValue Root;
4427 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004428
Chris Lattner8047d9a2009-12-24 00:37:38 +00004429 // Do not serialize (non-volatile) loads of constant memory with anything.
4430 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4431 Root = Builder.DAG.getEntryNode();
4432 ConstantMemory = true;
4433 } else {
4434 // Do not serialize non-volatile loads against each other.
4435 Root = Builder.DAG.getRoot();
4436 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004437
Chris Lattner8047d9a2009-12-24 00:37:38 +00004438 SDValue Ptr = Builder.getValue(PtrVal);
4439 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4440 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004441 false /*volatile*/,
4442 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004443
Chris Lattner8047d9a2009-12-24 00:37:38 +00004444 if (!ConstantMemory)
4445 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4446 return LoadVal;
4447}
4448
4449
4450/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4451/// If so, return true and lower it, otherwise return false and it will be
4452/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004453bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004454 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4455 if (I.getNumOperands() != 4)
4456 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004457
Eric Christopher551754c2010-04-16 23:37:20 +00004458 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004459 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004460 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004461 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004462 return false;
4463
Eric Christopher551754c2010-04-16 23:37:20 +00004464 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004465
Chris Lattner8047d9a2009-12-24 00:37:38 +00004466 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4467 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004468 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4469 bool ActuallyDoIt = true;
4470 MVT LoadVT;
4471 const Type *LoadTy;
4472 switch (Size->getZExtValue()) {
4473 default:
4474 LoadVT = MVT::Other;
4475 LoadTy = 0;
4476 ActuallyDoIt = false;
4477 break;
4478 case 2:
4479 LoadVT = MVT::i16;
4480 LoadTy = Type::getInt16Ty(Size->getContext());
4481 break;
4482 case 4:
4483 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004484 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004485 break;
4486 case 8:
4487 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004488 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004489 break;
4490 /*
4491 case 16:
4492 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004493 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004494 LoadTy = VectorType::get(LoadTy, 4);
4495 break;
4496 */
4497 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004498
Chris Lattner04b091a2009-12-24 01:07:17 +00004499 // This turns into unaligned loads. We only do this if the target natively
4500 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4501 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004502
Chris Lattner04b091a2009-12-24 01:07:17 +00004503 // Require that we can find a legal MVT, and only do this if the target
4504 // supports unaligned loads of that type. Expanding into byte loads would
4505 // bloat the code.
4506 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4507 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4508 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4509 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4510 ActuallyDoIt = false;
4511 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004512
Chris Lattner04b091a2009-12-24 01:07:17 +00004513 if (ActuallyDoIt) {
4514 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4515 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004516
Chris Lattner04b091a2009-12-24 01:07:17 +00004517 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4518 ISD::SETNE);
4519 EVT CallVT = TLI.getValueType(I.getType(), true);
4520 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4521 return true;
4522 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004523 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004524
4525
Chris Lattner8047d9a2009-12-24 00:37:38 +00004526 return false;
4527}
4528
4529
Dan Gohman46510a72010-04-15 01:51:59 +00004530void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004531 const char *RenameFn = 0;
4532 if (Function *F = I.getCalledFunction()) {
4533 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004534 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004535 if (II) {
4536 if (unsigned IID = II->getIntrinsicID(F)) {
4537 RenameFn = visitIntrinsicCall(I, IID);
4538 if (!RenameFn)
4539 return;
4540 }
4541 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542 if (unsigned IID = F->getIntrinsicID()) {
4543 RenameFn = visitIntrinsicCall(I, IID);
4544 if (!RenameFn)
4545 return;
4546 }
4547 }
4548
4549 // Check for well-known libc/libm calls. If the function is internal, it
4550 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004551 if (!F->hasLocalLinkage() && F->hasName()) {
4552 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004553 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004555 I.getOperand(1)->getType()->isFloatingPointTy() &&
4556 I.getType() == I.getOperand(1)->getType() &&
4557 I.getType() == I.getOperand(2)->getType()) {
4558 SDValue LHS = getValue(I.getOperand(1));
4559 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004560 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4561 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004562 return;
4563 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004564 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004565 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004566 I.getOperand(1)->getType()->isFloatingPointTy() &&
4567 I.getType() == I.getOperand(1)->getType()) {
4568 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004569 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4570 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004571 return;
4572 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004573 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004574 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004575 I.getOperand(1)->getType()->isFloatingPointTy() &&
4576 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004577 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004578 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004579 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4580 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004581 return;
4582 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004583 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004584 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004585 I.getOperand(1)->getType()->isFloatingPointTy() &&
4586 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004587 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004588 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004589 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4590 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004591 return;
4592 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004593 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4594 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004595 I.getOperand(1)->getType()->isFloatingPointTy() &&
4596 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004597 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004598 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004599 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4600 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004601 return;
4602 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004603 } else if (Name == "memcmp") {
4604 if (visitMemCmpCall(I))
4605 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004606 }
4607 }
Eric Christopher551754c2010-04-16 23:37:20 +00004608 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004609 visitInlineAsm(&I);
4610 return;
4611 }
4612
4613 SDValue Callee;
4614 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004615 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004616 else
Bill Wendling056292f2008-09-16 21:48:12 +00004617 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004618
Bill Wendling0d580132009-12-23 01:28:19 +00004619 // Check if we can potentially perform a tail call. More detailed checking is
4620 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004621 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004622}
4623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004625/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004626/// Chain/Flag as the input and updates them for the output Chain/Flag.
4627/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004628SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004629 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004630 // Assemble the legal parts into the final values.
4631 SmallVector<SDValue, 4> Values(ValueVTs.size());
4632 SmallVector<SDValue, 8> Parts;
4633 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4634 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004635 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004636 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004637 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004638
4639 Parts.resize(NumRegs);
4640 for (unsigned i = 0; i != NumRegs; ++i) {
4641 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004642 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004643 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004644 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004645 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004646 *Flag = P.getValue(2);
4647 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004649 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004650
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004651 // If the source register was virtual and if we know something about it,
4652 // add an assert node.
4653 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4654 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4655 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4656 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4657 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4658 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004659
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004660 unsigned RegSize = RegisterVT.getSizeInBits();
4661 unsigned NumSignBits = LOI.NumSignBits;
4662 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 // FIXME: We capture more information than the dag can represent. For
4665 // now, just use the tightest assertzext/assertsext possible.
4666 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004667 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004669 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004670 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004673 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004674 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004675 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004677 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004678 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004679 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004680 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004681 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004682 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004683 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004684
Bill Wendling4533cac2010-01-28 21:51:40 +00004685 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004686 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004687 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688 }
4689 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 Parts[i] = P;
4692 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004693
Bill Wendling46ada192010-03-02 01:55:18 +00004694 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004695 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004696 Part += NumRegs;
4697 Parts.clear();
4698 }
4699
Bill Wendling4533cac2010-01-28 21:51:40 +00004700 return DAG.getNode(ISD::MERGE_VALUES, dl,
4701 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4702 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703}
4704
4705/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004706/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707/// Chain/Flag as the input and updates them for the output Chain/Flag.
4708/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004709void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004710 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004711 // Get the list of the values's legal parts.
4712 unsigned NumRegs = Regs.size();
4713 SmallVector<SDValue, 8> Parts(NumRegs);
4714 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004715 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004716 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004717 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004718
Bill Wendling46ada192010-03-02 01:55:18 +00004719 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004720 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004721 &Parts[Part], NumParts, RegisterVT);
4722 Part += NumParts;
4723 }
4724
4725 // Copy the parts into the registers.
4726 SmallVector<SDValue, 8> Chains(NumRegs);
4727 for (unsigned i = 0; i != NumRegs; ++i) {
4728 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004729 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004730 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004731 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004732 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004733 *Flag = Part.getValue(1);
4734 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004735
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004736 Chains[i] = Part.getValue(0);
4737 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004738
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004740 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004741 // flagged to it. That is the CopyToReg nodes and the user are considered
4742 // a single scheduling unit. If we create a TokenFactor and return it as
4743 // chain, then the TokenFactor is both a predecessor (operand) of the
4744 // user as well as a successor (the TF operands are flagged to the user).
4745 // c1, f1 = CopyToReg
4746 // c2, f2 = CopyToReg
4747 // c3 = TokenFactor c1, c2
4748 // ...
4749 // = op c3, ..., f2
4750 Chain = Chains[NumRegs-1];
4751 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004752 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004753}
4754
4755/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004756/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004758void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4759 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004760 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004762 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004763 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004764 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004765 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004766 Ops.push_back(Res);
4767
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004769 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004770 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004771 for (unsigned i = 0; i != NumRegs; ++i) {
4772 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004773 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004774 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 }
4776}
4777
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004778/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779/// i.e. it isn't a stack pointer or some other special register, return the
4780/// register class for the register. Otherwise, return null.
4781static const TargetRegisterClass *
4782isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4783 const TargetLowering &TLI,
4784 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004786 const TargetRegisterClass *FoundRC = 0;
4787 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4788 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790
4791 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004792 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4794 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4795 I != E; ++I) {
4796 if (TLI.isTypeLegal(*I)) {
4797 // If we have already found this register in a different register class,
4798 // choose the one with the largest VT specified. For example, on
4799 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004801 ThisVT = *I;
4802 break;
4803 }
4804 }
4805 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004806
Owen Anderson825b72b2009-08-11 20:47:22 +00004807 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004808
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004809 // NOTE: This isn't ideal. In particular, this might allocate the
4810 // frame pointer in functions that need it (due to them not being taken
4811 // out of allocation, because a variable sized allocation hasn't been seen
4812 // yet). This is a slight code pessimization, but should still work.
4813 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4814 E = RC->allocation_order_end(MF); I != E; ++I)
4815 if (*I == Reg) {
4816 // We found a matching register class. Keep looking at others in case
4817 // we find one with larger registers that this physreg is also in.
4818 FoundRC = RC;
4819 FoundVT = ThisVT;
4820 break;
4821 }
4822 }
4823 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004824}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004825
4826
4827namespace llvm {
4828/// AsmOperandInfo - This contains information for each constraint that we are
4829/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004830class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004831 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004832public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004833 /// CallOperand - If this is the result output operand or a clobber
4834 /// this is null, otherwise it is the incoming operand to the CallInst.
4835 /// This gets modified as the asm is processed.
4836 SDValue CallOperand;
4837
4838 /// AssignedRegs - If this is a register or register class operand, this
4839 /// contains the set of register corresponding to the operand.
4840 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004842 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4843 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4844 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004846 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4847 /// busy in OutputRegs/InputRegs.
4848 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004849 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850 std::set<unsigned> &InputRegs,
4851 const TargetRegisterInfo &TRI) const {
4852 if (isOutReg) {
4853 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4854 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4855 }
4856 if (isInReg) {
4857 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4858 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4859 }
4860 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004861
Owen Andersone50ed302009-08-10 22:56:29 +00004862 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004863 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004865 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004866 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004867 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004869
Chris Lattner81249c92008-10-17 17:05:25 +00004870 if (isa<BasicBlock>(CallOperandVal))
4871 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004872
Chris Lattner81249c92008-10-17 17:05:25 +00004873 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004874
Chris Lattner81249c92008-10-17 17:05:25 +00004875 // If this is an indirect operand, the operand is a pointer to the
4876 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004877 if (isIndirect) {
4878 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4879 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004880 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004881 OpTy = PtrTy->getElementType();
4882 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004883
Chris Lattner81249c92008-10-17 17:05:25 +00004884 // If OpTy is not a single value, it may be a struct/union that we
4885 // can tile with integers.
4886 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4887 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4888 switch (BitSize) {
4889 default: break;
4890 case 1:
4891 case 8:
4892 case 16:
4893 case 32:
4894 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004895 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004896 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004897 break;
4898 }
4899 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004900
Chris Lattner81249c92008-10-17 17:05:25 +00004901 return TLI.getValueType(OpTy, true);
4902 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004903
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004904private:
4905 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4906 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004907 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004908 const TargetRegisterInfo &TRI) {
4909 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4910 Regs.insert(Reg);
4911 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4912 for (; *Aliases; ++Aliases)
4913 Regs.insert(*Aliases);
4914 }
4915};
4916} // end llvm namespace.
4917
4918
4919/// GetRegistersForValue - Assign registers (virtual or physical) for the
4920/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004921/// register allocator to handle the assignment process. However, if the asm
4922/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004923/// allocation. This produces generally horrible, but correct, code.
4924///
4925/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004926/// Input and OutputRegs are the set of already allocated physical registers.
4927///
Dan Gohman2048b852009-11-23 18:04:58 +00004928void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004929GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004930 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004931 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004932 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004934 // Compute whether this value requires an input register, an output register,
4935 // or both.
4936 bool isOutReg = false;
4937 bool isInReg = false;
4938 switch (OpInfo.Type) {
4939 case InlineAsm::isOutput:
4940 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004941
4942 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004943 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004944 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004945 break;
4946 case InlineAsm::isInput:
4947 isInReg = true;
4948 isOutReg = false;
4949 break;
4950 case InlineAsm::isClobber:
4951 isOutReg = true;
4952 isInReg = true;
4953 break;
4954 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004955
4956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004957 MachineFunction &MF = DAG.getMachineFunction();
4958 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004960 // If this is a constraint for a single physreg, or a constraint for a
4961 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004962 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004963 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4964 OpInfo.ConstraintVT);
4965
4966 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004967 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004968 // If this is a FP input in an integer register (or visa versa) insert a bit
4969 // cast of the input value. More generally, handle any case where the input
4970 // value disagrees with the register class we plan to stick this in.
4971 if (OpInfo.Type == InlineAsm::isInput &&
4972 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004973 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004974 // types are identical size, use a bitcast to convert (e.g. two differing
4975 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004976 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004977 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004978 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004979 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004980 OpInfo.ConstraintVT = RegVT;
4981 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4982 // If the input is a FP value and we want it in FP registers, do a
4983 // bitcast to the corresponding integer type. This turns an f64 value
4984 // into i64, which can be passed with two i32 values on a 32-bit
4985 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004986 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00004987 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004988 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004989 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004990 OpInfo.ConstraintVT = RegVT;
4991 }
4992 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004993
Owen Anderson23b9b192009-08-12 00:36:31 +00004994 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004995 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004996
Owen Andersone50ed302009-08-10 22:56:29 +00004997 EVT RegVT;
4998 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004999
5000 // If this is a constraint for a specific physical register, like {r17},
5001 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005002 if (unsigned AssignedReg = PhysReg.first) {
5003 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005004 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005005 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005006
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005007 // Get the actual register value type. This is important, because the user
5008 // may have asked for (e.g.) the AX register in i32 type. We need to
5009 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005010 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005011
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005012 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005013 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005014
5015 // If this is an expanded reference, add the rest of the regs to Regs.
5016 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005017 TargetRegisterClass::iterator I = RC->begin();
5018 for (; *I != AssignedReg; ++I)
5019 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005021 // Already added the first reg.
5022 --NumRegs; ++I;
5023 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005024 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005025 Regs.push_back(*I);
5026 }
5027 }
Bill Wendling651ad132009-12-22 01:25:10 +00005028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005029 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5030 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5031 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5032 return;
5033 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005035 // Otherwise, if this was a reference to an LLVM register class, create vregs
5036 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005037 if (const TargetRegisterClass *RC = PhysReg.second) {
5038 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005039 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005040 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041
Evan Chengfb112882009-03-23 08:01:15 +00005042 // Create the appropriate number of virtual registers.
5043 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5044 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005045 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005046
Evan Chengfb112882009-03-23 08:01:15 +00005047 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5048 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005049 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005050
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005051 // This is a reference to a register class that doesn't directly correspond
5052 // to an LLVM register class. Allocate NumRegs consecutive, available,
5053 // registers from the class.
5054 std::vector<unsigned> RegClassRegs
5055 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5056 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5059 unsigned NumAllocated = 0;
5060 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5061 unsigned Reg = RegClassRegs[i];
5062 // See if this register is available.
5063 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5064 (isInReg && InputRegs.count(Reg))) { // Already used.
5065 // Make sure we find consecutive registers.
5066 NumAllocated = 0;
5067 continue;
5068 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005069
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005070 // Check to see if this register is allocatable (i.e. don't give out the
5071 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005072 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5073 if (!RC) { // Couldn't allocate this register.
5074 // Reset NumAllocated to make sure we return consecutive registers.
5075 NumAllocated = 0;
5076 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005077 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005078
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079 // Okay, this register is good, we can use it.
5080 ++NumAllocated;
5081
5082 // If we allocated enough consecutive registers, succeed.
5083 if (NumAllocated == NumRegs) {
5084 unsigned RegStart = (i-NumAllocated)+1;
5085 unsigned RegEnd = i+1;
5086 // Mark all of the allocated registers used.
5087 for (unsigned i = RegStart; i != RegEnd; ++i)
5088 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005089
5090 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091 OpInfo.ConstraintVT);
5092 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5093 return;
5094 }
5095 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005097 // Otherwise, we couldn't allocate enough registers for this.
5098}
5099
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005100/// visitInlineAsm - Handle a call to an InlineAsm object.
5101///
Dan Gohman46510a72010-04-15 01:51:59 +00005102void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5103 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005104
5105 /// ConstraintOperands - Information about all of the constraints.
5106 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005108 std::set<unsigned> OutputRegs, InputRegs;
5109
5110 // Do a prepass over the constraints, canonicalizing them, and building up the
5111 // ConstraintOperands list.
5112 std::vector<InlineAsm::ConstraintInfo>
5113 ConstraintInfos = IA->ParseConstraints();
5114
Evan Chengda43bcf2008-09-24 00:05:32 +00005115 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005116
Chris Lattner6c147292009-04-30 00:48:50 +00005117 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005118
Chris Lattner6c147292009-04-30 00:48:50 +00005119 // We won't need to flush pending loads if this asm doesn't touch
5120 // memory and is nonvolatile.
5121 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005122 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005123 else
5124 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005126 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5127 unsigned ResNo = 0; // ResNo - The result number of the next output.
5128 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5129 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5130 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005131
Owen Anderson825b72b2009-08-11 20:47:22 +00005132 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005133
5134 // Compute the value type for each operand.
5135 switch (OpInfo.Type) {
5136 case InlineAsm::isOutput:
5137 // Indirect outputs just consume an argument.
5138 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005139 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005140 break;
5141 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005143 // The return value of the call is this value. As such, there is no
5144 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005145 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005146 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5148 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5149 } else {
5150 assert(ResNo == 0 && "Asm only has one result!");
5151 OpVT = TLI.getValueType(CS.getType());
5152 }
5153 ++ResNo;
5154 break;
5155 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005156 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005157 break;
5158 case InlineAsm::isClobber:
5159 // Nothing to do.
5160 break;
5161 }
5162
5163 // If this is an input or an indirect output, process the call argument.
5164 // BasicBlocks are labels, currently appearing only in asm's.
5165 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005166 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005167 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5168
Dan Gohman46510a72010-04-15 01:51:59 +00005169 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005171 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005172 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005173 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005174
Owen Anderson1d0be152009-08-13 21:58:54 +00005175 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005176 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005179 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005180
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005181 // Second pass over the constraints: compute which constraint option to use
5182 // and assign registers to constraints that want a specific physreg.
5183 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5184 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005185
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005186 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005187 // matching input. If their types mismatch, e.g. one is an integer, the
5188 // other is floating point, or their sizes are different, flag it as an
5189 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005190 if (OpInfo.hasMatchingInput()) {
5191 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005192
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005193 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005194 if ((OpInfo.ConstraintVT.isInteger() !=
5195 Input.ConstraintVT.isInteger()) ||
5196 (OpInfo.ConstraintVT.getSizeInBits() !=
5197 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005198 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005199 " with a matching output constraint of"
5200 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005201 }
5202 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005203 }
5204 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005205
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005206 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005207 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005209 // If this is a memory input, and if the operand is not indirect, do what we
5210 // need to to provide an address for the memory input.
5211 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5212 !OpInfo.isIndirect) {
5213 assert(OpInfo.Type == InlineAsm::isInput &&
5214 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005216 // Memory operands really want the address of the value. If we don't have
5217 // an indirect input, put it in the constpool if we can, otherwise spill
5218 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // If the operand is a float, integer, or vector constant, spill to a
5221 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005222 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5224 isa<ConstantVector>(OpVal)) {
5225 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5226 TLI.getPointerTy());
5227 } else {
5228 // Otherwise, create a stack slot and emit a store to it before the
5229 // asm.
5230 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005231 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005232 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5233 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005234 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005235 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005236 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005237 OpInfo.CallOperand, StackSlot, NULL, 0,
5238 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005239 OpInfo.CallOperand = StackSlot;
5240 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005241
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005242 // There is no longer a Value* corresponding to this operand.
5243 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005245 // It is now an indirect operand.
5246 OpInfo.isIndirect = true;
5247 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005248
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005249 // If this constraint is for a specific register, allocate it before
5250 // anything else.
5251 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005252 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005253 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005254
Bill Wendling651ad132009-12-22 01:25:10 +00005255 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005258 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5260 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 // C_Register operands have already been allocated, Other/Memory don't need
5263 // to be.
5264 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005265 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005266 }
5267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5269 std::vector<SDValue> AsmNodeOperands;
5270 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5271 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005272 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5273 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005274
Chris Lattnerdecc2672010-04-07 05:20:54 +00005275 // If we have a !srcloc metadata node associated with it, we want to attach
5276 // this to the ultimately generated inline asm machineinstr. To do this, we
5277 // pass in the third operand as this (potentially null) inline asm MDNode.
5278 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5279 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005280
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005281 // Loop over all of the inputs, copying the operand values into the
5282 // appropriate registers and processing the output regs.
5283 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005284
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005285 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5286 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005288 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5289 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5290
5291 switch (OpInfo.Type) {
5292 case InlineAsm::isOutput: {
5293 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5294 OpInfo.ConstraintType != TargetLowering::C_Register) {
5295 // Memory output, or 'other' output (e.g. 'X' constraint).
5296 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5297
5298 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005299 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5300 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301 TLI.getPointerTy()));
5302 AsmNodeOperands.push_back(OpInfo.CallOperand);
5303 break;
5304 }
5305
5306 // Otherwise, this is a register or register class output.
5307
5308 // Copy the output from the appropriate register. Find a register that
5309 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005310 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005311 report_fatal_error("Couldn't allocate output reg for constraint '" +
5312 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313
5314 // If this is an indirect operand, store through the pointer after the
5315 // asm.
5316 if (OpInfo.isIndirect) {
5317 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5318 OpInfo.CallOperandVal));
5319 } else {
5320 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005321 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005322 // Concatenate this output onto the outputs list.
5323 RetValRegs.append(OpInfo.AssignedRegs);
5324 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005326 // Add information to the INLINEASM node to know that this register is
5327 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005328 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005329 InlineAsm::Kind_RegDefEarlyClobber :
5330 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005331 false,
5332 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005333 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005334 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 break;
5336 }
5337 case InlineAsm::isInput: {
5338 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005339
Chris Lattner6bdcda32008-10-17 16:47:46 +00005340 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005341 // If this is required to match an output register we have already set,
5342 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005343 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005344
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 // Scan until we find the definition we already emitted of this operand.
5346 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005347 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005348 for (; OperandNo; --OperandNo) {
5349 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005350 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005351 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005352 assert((InlineAsm::isRegDefKind(OpFlag) ||
5353 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5354 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005355 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005356 }
5357
Evan Cheng697cbbf2009-03-20 18:03:34 +00005358 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005359 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005360 if (InlineAsm::isRegDefKind(OpFlag) ||
5361 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005362 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005363 if (OpInfo.isIndirect) {
5364 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005365 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005366 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5367 " don't know how to handle tied "
5368 "indirect register inputs");
5369 }
5370
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005371 RegsForValue MatchedRegs;
5372 MatchedRegs.TLI = &TLI;
5373 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005374 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005375 MatchedRegs.RegVTs.push_back(RegVT);
5376 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005377 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005378 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005379 MatchedRegs.Regs.push_back
5380 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005381
5382 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005383 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005384 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005385 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005386 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005387 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005389 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005390
5391 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5392 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5393 "Unexpected number of operands");
5394 // Add information to the INLINEASM node to know about this input.
5395 // See InlineAsm.h isUseOperandTiedToDef.
5396 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5397 OpInfo.getMatchedOperand());
5398 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5399 TLI.getPointerTy()));
5400 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5401 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005405 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005406 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005407
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005408 std::vector<SDValue> Ops;
5409 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005410 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005411 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005412 report_fatal_error("Invalid operand for inline asm constraint '" +
5413 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005414
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005416 unsigned ResOpType =
5417 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005418 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005419 TLI.getPointerTy()));
5420 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5421 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005422 }
5423
5424 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5426 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5427 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005429 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005430 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005431 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005432 TLI.getPointerTy()));
5433 AsmNodeOperands.push_back(InOperandVal);
5434 break;
5435 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005436
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005437 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5438 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5439 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005440 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005441 "Don't know how to handle indirect register inputs yet!");
5442
5443 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005444 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005445 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005446 report_fatal_error("Couldn't allocate input reg for constraint '" +
5447 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448
Dale Johannesen66978ee2009-01-31 02:22:37 +00005449 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005450 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005451
Chris Lattnerdecc2672010-04-07 05:20:54 +00005452 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005453 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005454 break;
5455 }
5456 case InlineAsm::isClobber: {
5457 // Add the clobbered value to the operand list, so that the register
5458 // allocator is aware that the physreg got clobbered.
5459 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005460 OpInfo.AssignedRegs.AddInlineAsmOperands(
5461 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005462 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005463 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464 break;
5465 }
5466 }
5467 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005468
Chris Lattnerdecc2672010-04-07 05:20:54 +00005469 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005470 AsmNodeOperands[0] = Chain;
5471 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005472
Dale Johannesen66978ee2009-01-31 02:22:37 +00005473 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005474 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005475 &AsmNodeOperands[0], AsmNodeOperands.size());
5476 Flag = Chain.getValue(1);
5477
5478 // If this asm returns a register value, copy the result from that register
5479 // and set it as the value of the call.
5480 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005481 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005482 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005483
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005484 // FIXME: Why don't we do this for inline asms with MRVs?
5485 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005486 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005487
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005488 // If any of the results of the inline asm is a vector, it may have the
5489 // wrong width/num elts. This can happen for register classes that can
5490 // contain multiple different value types. The preg or vreg allocated may
5491 // not have the same VT as was expected. Convert it to the right type
5492 // with bit_convert.
5493 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005494 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005495 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005496
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005497 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005498 ResultType.isInteger() && Val.getValueType().isInteger()) {
5499 // If a result value was tied to an input value, the computed result may
5500 // have a wider width than the expected result. Extract the relevant
5501 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005502 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005503 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005504
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005505 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005506 }
Dan Gohman95915732008-10-18 01:03:45 +00005507
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005508 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005509 // Don't need to use this as a chain in this case.
5510 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5511 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005512 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005513
Dan Gohman46510a72010-04-15 01:51:59 +00005514 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005515
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 // Process indirect outputs, first output all of the flagged copies out of
5517 // physregs.
5518 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5519 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005520 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005521 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005522 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005523 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5524 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005525
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005526 // Emit the non-flagged stores from the physregs.
5527 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005528 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5529 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5530 StoresToEmit[i].first,
5531 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005532 StoresToEmit[i].second, 0,
5533 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005534 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005535 }
5536
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005537 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005538 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005539 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 DAG.setRoot(Chain);
5542}
5543
Dan Gohman46510a72010-04-15 01:51:59 +00005544void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005545 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5546 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005547 getValue(I.getOperand(1)),
5548 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549}
5550
Dan Gohman46510a72010-04-15 01:51:59 +00005551void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005552 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5553 getRoot(), getValue(I.getOperand(0)),
5554 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005555 setValue(&I, V);
5556 DAG.setRoot(V.getValue(1));
5557}
5558
Dan Gohman46510a72010-04-15 01:51:59 +00005559void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005560 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5561 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005562 getValue(I.getOperand(1)),
5563 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005564}
5565
Dan Gohman46510a72010-04-15 01:51:59 +00005566void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005567 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5568 MVT::Other, getRoot(),
5569 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005570 getValue(I.getOperand(2)),
5571 DAG.getSrcValue(I.getOperand(1)),
5572 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005573}
5574
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005575/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005576/// implementation, which just calls LowerCall.
5577/// FIXME: When all targets are
5578/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005579std::pair<SDValue, SDValue>
5580TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5581 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005582 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005583 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005584 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005585 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005586 ArgListTy &Args, SelectionDAG &DAG,
5587 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005588 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005589 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005590 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005591 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005592 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5593 for (unsigned Value = 0, NumValues = ValueVTs.size();
5594 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005595 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005596 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005597 SDValue Op = SDValue(Args[i].Node.getNode(),
5598 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005599 ISD::ArgFlagsTy Flags;
5600 unsigned OriginalAlignment =
5601 getTargetData()->getABITypeAlignment(ArgTy);
5602
5603 if (Args[i].isZExt)
5604 Flags.setZExt();
5605 if (Args[i].isSExt)
5606 Flags.setSExt();
5607 if (Args[i].isInReg)
5608 Flags.setInReg();
5609 if (Args[i].isSRet)
5610 Flags.setSRet();
5611 if (Args[i].isByVal) {
5612 Flags.setByVal();
5613 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5614 const Type *ElementTy = Ty->getElementType();
5615 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005616 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005617 // For ByVal, alignment should come from FE. BE will guess if this
5618 // info is not there but there are cases it cannot get right.
5619 if (Args[i].Alignment)
5620 FrameAlign = Args[i].Alignment;
5621 Flags.setByValAlign(FrameAlign);
5622 Flags.setByValSize(FrameSize);
5623 }
5624 if (Args[i].isNest)
5625 Flags.setNest();
5626 Flags.setOrigAlign(OriginalAlignment);
5627
Owen Anderson23b9b192009-08-12 00:36:31 +00005628 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5629 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005630 SmallVector<SDValue, 4> Parts(NumParts);
5631 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5632
5633 if (Args[i].isSExt)
5634 ExtendKind = ISD::SIGN_EXTEND;
5635 else if (Args[i].isZExt)
5636 ExtendKind = ISD::ZERO_EXTEND;
5637
Bill Wendling46ada192010-03-02 01:55:18 +00005638 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005639 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005640
Dan Gohman98ca4f22009-08-05 01:29:28 +00005641 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005642 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005643 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5644 if (NumParts > 1 && j == 0)
5645 MyFlags.Flags.setSplit();
5646 else if (j != 0)
5647 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005648
Dan Gohman98ca4f22009-08-05 01:29:28 +00005649 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005650 }
5651 }
5652 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005653
Dan Gohman98ca4f22009-08-05 01:29:28 +00005654 // Handle the incoming return values from the call.
5655 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005656 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005657 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005658 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005659 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005660 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5661 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005662 for (unsigned i = 0; i != NumRegs; ++i) {
5663 ISD::InputArg MyFlags;
5664 MyFlags.VT = RegisterVT;
5665 MyFlags.Used = isReturnValueUsed;
5666 if (RetSExt)
5667 MyFlags.Flags.setSExt();
5668 if (RetZExt)
5669 MyFlags.Flags.setZExt();
5670 if (isInreg)
5671 MyFlags.Flags.setInReg();
5672 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005673 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005674 }
5675
Dan Gohman98ca4f22009-08-05 01:29:28 +00005676 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005677 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005678 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005679
5680 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005681 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005682 "LowerCall didn't return a valid chain!");
5683 assert((!isTailCall || InVals.empty()) &&
5684 "LowerCall emitted a return value for a tail call!");
5685 assert((isTailCall || InVals.size() == Ins.size()) &&
5686 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005687
5688 // For a tail call, the return value is merely live-out and there aren't
5689 // any nodes in the DAG representing it. Return a special value to
5690 // indicate that a tail call has been emitted and no more Instructions
5691 // should be processed in the current block.
5692 if (isTailCall) {
5693 DAG.setRoot(Chain);
5694 return std::make_pair(SDValue(), SDValue());
5695 }
5696
Evan Chengaf1871f2010-03-11 19:38:18 +00005697 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5698 assert(InVals[i].getNode() &&
5699 "LowerCall emitted a null value!");
5700 assert(Ins[i].VT == InVals[i].getValueType() &&
5701 "LowerCall emitted a value with the wrong type!");
5702 });
5703
Dan Gohman98ca4f22009-08-05 01:29:28 +00005704 // Collect the legal value parts into potentially illegal values
5705 // that correspond to the original function's return values.
5706 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5707 if (RetSExt)
5708 AssertOp = ISD::AssertSext;
5709 else if (RetZExt)
5710 AssertOp = ISD::AssertZext;
5711 SmallVector<SDValue, 4> ReturnValues;
5712 unsigned CurReg = 0;
5713 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005714 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005715 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5716 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005717
Bill Wendling46ada192010-03-02 01:55:18 +00005718 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005719 NumRegs, RegisterVT, VT,
5720 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005721 CurReg += NumRegs;
5722 }
5723
5724 // For a function returning void, there is no return value. We can't create
5725 // such a node, so we just return a null return value in that case. In
5726 // that case, nothing will actualy look at the value.
5727 if (ReturnValues.empty())
5728 return std::make_pair(SDValue(), Chain);
5729
5730 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5731 DAG.getVTList(&RetTys[0], RetTys.size()),
5732 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005733 return std::make_pair(Res, Chain);
5734}
5735
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005736void TargetLowering::LowerOperationWrapper(SDNode *N,
5737 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005738 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005739 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005740 if (Res.getNode())
5741 Results.push_back(Res);
5742}
5743
Dan Gohmand858e902010-04-17 15:26:15 +00005744SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005745 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005746 return SDValue();
5747}
5748
Dan Gohman46510a72010-04-15 01:51:59 +00005749void
5750SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005751 SDValue Op = getValue(V);
5752 assert((Op.getOpcode() != ISD::CopyFromReg ||
5753 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5754 "Copy from a reg to the same reg!");
5755 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5756
Owen Anderson23b9b192009-08-12 00:36:31 +00005757 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005759 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005760 PendingExports.push_back(Chain);
5761}
5762
5763#include "llvm/CodeGen/SelectionDAGISel.h"
5764
Dan Gohman46510a72010-04-15 01:51:59 +00005765void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005766 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005767 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005768 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005769 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005770 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005771 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005772 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005773
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005774 // Check whether the function can return without sret-demotion.
5775 SmallVector<EVT, 4> OutVTs;
5776 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005777 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005778 OutVTs, OutsFlags, TLI);
5779 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5780
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005781 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005782 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005783 if (!FLI.CanLowerReturn) {
5784 // Put in an sret pointer parameter before all the other parameters.
5785 SmallVector<EVT, 1> ValueVTs;
5786 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5787
5788 // NOTE: Assuming that a pointer will never break down to more than one VT
5789 // or one register.
5790 ISD::ArgFlagsTy Flags;
5791 Flags.setSRet();
5792 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5793 ISD::InputArg RetArg(Flags, RegisterVT, true);
5794 Ins.push_back(RetArg);
5795 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005796
Dan Gohman98ca4f22009-08-05 01:29:28 +00005797 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005798 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005799 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005800 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005801 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005802 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5803 bool isArgValueUsed = !I->use_empty();
5804 for (unsigned Value = 0, NumValues = ValueVTs.size();
5805 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005806 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005807 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005808 ISD::ArgFlagsTy Flags;
5809 unsigned OriginalAlignment =
5810 TD->getABITypeAlignment(ArgTy);
5811
5812 if (F.paramHasAttr(Idx, Attribute::ZExt))
5813 Flags.setZExt();
5814 if (F.paramHasAttr(Idx, Attribute::SExt))
5815 Flags.setSExt();
5816 if (F.paramHasAttr(Idx, Attribute::InReg))
5817 Flags.setInReg();
5818 if (F.paramHasAttr(Idx, Attribute::StructRet))
5819 Flags.setSRet();
5820 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5821 Flags.setByVal();
5822 const PointerType *Ty = cast<PointerType>(I->getType());
5823 const Type *ElementTy = Ty->getElementType();
5824 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5825 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5826 // For ByVal, alignment should be passed from FE. BE will guess if
5827 // this info is not there but there are cases it cannot get right.
5828 if (F.getParamAlignment(Idx))
5829 FrameAlign = F.getParamAlignment(Idx);
5830 Flags.setByValAlign(FrameAlign);
5831 Flags.setByValSize(FrameSize);
5832 }
5833 if (F.paramHasAttr(Idx, Attribute::Nest))
5834 Flags.setNest();
5835 Flags.setOrigAlign(OriginalAlignment);
5836
Owen Anderson23b9b192009-08-12 00:36:31 +00005837 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5838 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005839 for (unsigned i = 0; i != NumRegs; ++i) {
5840 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5841 if (NumRegs > 1 && i == 0)
5842 MyFlags.Flags.setSplit();
5843 // if it isn't first piece, alignment must be 1
5844 else if (i > 0)
5845 MyFlags.Flags.setOrigAlign(1);
5846 Ins.push_back(MyFlags);
5847 }
5848 }
5849 }
5850
5851 // Call the target to set up the argument values.
5852 SmallVector<SDValue, 8> InVals;
5853 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5854 F.isVarArg(), Ins,
5855 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005856
5857 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005859 "LowerFormalArguments didn't return a valid chain!");
5860 assert(InVals.size() == Ins.size() &&
5861 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005862 DEBUG({
5863 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5864 assert(InVals[i].getNode() &&
5865 "LowerFormalArguments emitted a null value!");
5866 assert(Ins[i].VT == InVals[i].getValueType() &&
5867 "LowerFormalArguments emitted a value with the wrong type!");
5868 }
5869 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005870
Dan Gohman5e866062009-08-06 15:37:27 +00005871 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005872 DAG.setRoot(NewRoot);
5873
5874 // Set up the argument values.
5875 unsigned i = 0;
5876 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005877 if (!FLI.CanLowerReturn) {
5878 // Create a virtual register for the sret pointer, and put in a copy
5879 // from the sret argument into it.
5880 SmallVector<EVT, 1> ValueVTs;
5881 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5882 EVT VT = ValueVTs[0];
5883 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5884 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005885 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005886 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005887
Dan Gohman2048b852009-11-23 18:04:58 +00005888 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005889 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5890 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5891 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005892 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5893 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005894 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005895
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005896 // i indexes lowered arguments. Bump it past the hidden sret argument.
5897 // Idx indexes LLVM arguments. Don't touch it.
5898 ++i;
5899 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005900
Dan Gohman46510a72010-04-15 01:51:59 +00005901 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005902 ++I, ++Idx) {
5903 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005904 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005905 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005906 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005907 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005908 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005909 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5910 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005911
5912 if (!I->use_empty()) {
5913 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5914 if (F.paramHasAttr(Idx, Attribute::SExt))
5915 AssertOp = ISD::AssertSext;
5916 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5917 AssertOp = ISD::AssertZext;
5918
Bill Wendling46ada192010-03-02 01:55:18 +00005919 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00005920 NumParts, PartVT, VT,
5921 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005922 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005923
Dan Gohman98ca4f22009-08-05 01:29:28 +00005924 i += NumParts;
5925 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005926
Dan Gohman98ca4f22009-08-05 01:29:28 +00005927 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00005928 SDValue Res;
5929 if (!ArgValues.empty())
5930 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
5931 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00005932 SDB->setValue(I, Res);
5933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 // If this argument is live outside of the entry block, insert a copy from
5935 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005936 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005937 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005938 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005939
Dan Gohman98ca4f22009-08-05 01:29:28 +00005940 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005941
5942 // Finally, if the target has anything special to do, allow it to do so.
5943 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00005944 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945}
5946
5947/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5948/// ensure constants are generated when needed. Remember the virtual registers
5949/// that need to be added to the Machine PHI nodes as input. We cannot just
5950/// directly add them, because expansion might result in multiple MBB's for one
5951/// BB. As such, the start of the BB might correspond to a different MBB than
5952/// the end.
5953///
5954void
Dan Gohman46510a72010-04-15 01:51:59 +00005955SelectionDAGISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
5956 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957
5958 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5959
5960 // Check successor nodes' PHI nodes that expect a constant to be available
5961 // from this block.
5962 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00005963 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005964 if (!isa<PHINode>(SuccBB->begin())) continue;
5965 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005967 // If this terminator has multiple identical successors (common for
5968 // switches), only handle each succ once.
5969 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005970
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005971 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005972
5973 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5974 // nodes and Machine PHI nodes, but the incoming operands have not been
5975 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00005976 for (BasicBlock::const_iterator I = SuccBB->begin();
5977 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005978 // Ignore dead phi's.
5979 if (PN->use_empty()) continue;
5980
5981 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00005982 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983
Dan Gohman46510a72010-04-15 01:51:59 +00005984 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005985 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005986 if (RegOut == 0) {
5987 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005988 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 }
5990 Reg = RegOut;
5991 } else {
5992 Reg = FuncInfo->ValueMap[PHIOp];
5993 if (Reg == 0) {
5994 assert(isa<AllocaInst>(PHIOp) &&
5995 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5996 "Didn't codegen value into a register!??");
5997 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00005998 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999 }
6000 }
6001
6002 // Remember that this register needs to added to the machine PHI node as
6003 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006004 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006005 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6006 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006007 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006008 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006010 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006011 Reg += NumRegisters;
6012 }
6013 }
6014 }
Dan Gohman2048b852009-11-23 18:04:58 +00006015 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006016}
6017
Dan Gohman3df24e62008-09-03 23:12:08 +00006018/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6019/// supports legal types, and it emits MachineInstrs directly instead of
6020/// creating SelectionDAG nodes.
6021///
6022bool
Dan Gohman46510a72010-04-15 01:51:59 +00006023SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(const BasicBlock *LLVMBB,
Dan Gohman3df24e62008-09-03 23:12:08 +00006024 FastISel *F) {
Dan Gohman46510a72010-04-15 01:51:59 +00006025 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006026
Dan Gohman3df24e62008-09-03 23:12:08 +00006027 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006028 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006029
6030 // Check successor nodes' PHI nodes that expect a constant to be available
6031 // from this block.
6032 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006033 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohman3df24e62008-09-03 23:12:08 +00006034 if (!isa<PHINode>(SuccBB->begin())) continue;
6035 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006036
Dan Gohman3df24e62008-09-03 23:12:08 +00006037 // If this terminator has multiple identical successors (common for
6038 // switches), only handle each succ once.
6039 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006040
Dan Gohman3df24e62008-09-03 23:12:08 +00006041 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohman3df24e62008-09-03 23:12:08 +00006042
6043 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6044 // nodes and Machine PHI nodes, but the incoming operands have not been
6045 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006046 for (BasicBlock::const_iterator I = SuccBB->begin();
6047 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohman3df24e62008-09-03 23:12:08 +00006048 // Ignore dead phi's.
6049 if (PN->use_empty()) continue;
6050
6051 // Only handle legal types. Two interesting things to note here. First,
6052 // by bailing out early, we may leave behind some dead instructions,
6053 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6054 // own moves. Second, this check is necessary becuase FastISel doesn't
6055 // use CreateRegForValue to create registers, so it always creates
6056 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006057 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006058 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6059 // Promote MVT::i1.
6060 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006061 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006062 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006063 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006064 return false;
6065 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006066 }
6067
Dan Gohman46510a72010-04-15 01:51:59 +00006068 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohman3df24e62008-09-03 23:12:08 +00006069
6070 unsigned Reg = F->getRegForValue(PHIOp);
6071 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006072 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006073 return false;
6074 }
Dan Gohman2048b852009-11-23 18:04:58 +00006075 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006076 }
6077 }
6078
6079 return true;
6080}