blob: 470689bd06f502df10ce1ee053b631781434ab75 [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 Gohman8ba3aa72010-04-20 00:48:35 +0000617 CurDebugLoc = I.getDebugLoc();
618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000619 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000620
621 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000622}
623
Dan Gohman46510a72010-04-15 01:51:59 +0000624void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000625 // Note: this doesn't use InstVisitor, because it has to work with
626 // ConstantExpr's in addition to instructions.
627 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000628 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629 // Build the switch statement using the Instruction.def file.
630#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000631 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000632#include "llvm/Instruction.def"
633 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000634
635 // Assign the ordering to the freshly created DAG nodes.
636 if (NodeMap.count(&I)) {
637 ++SDNodeOrder;
638 AssignOrderingToNode(getValue(&I).getNode());
639 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000640}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000641
Dan Gohman2048b852009-11-23 18:04:58 +0000642SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000643 SDValue &N = NodeMap[V];
644 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000645
Dan Gohman383b5f62010-04-17 15:32:28 +0000646 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000647 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000648
Dan Gohman383b5f62010-04-17 15:32:28 +0000649 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000650 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000651
Dan Gohman383b5f62010-04-17 15:32:28 +0000652 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000653 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000655 if (isa<ConstantPointerNull>(C))
656 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000657
Dan Gohman383b5f62010-04-17 15:32:28 +0000658 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000659 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000660
Nate Begeman9008ca62009-04-27 18:41:29 +0000661 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000662 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663
Dan Gohman383b5f62010-04-17 15:32:28 +0000664 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000665 visit(CE->getOpcode(), *CE);
666 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000667 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000668 return N1;
669 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000670
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000671 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
672 SmallVector<SDValue, 4> Constants;
673 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
674 OI != OE; ++OI) {
675 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000676 // If the operand is an empty aggregate, there are no values.
677 if (!Val) continue;
678 // Add each leaf value from the operand to the Constants list
679 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
681 Constants.push_back(SDValue(Val, i));
682 }
Bill Wendling87710f02009-12-21 23:47:40 +0000683
Bill Wendling4533cac2010-01-28 21:51:40 +0000684 return DAG.getMergeValues(&Constants[0], Constants.size(),
685 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000686 }
687
Duncan Sands1df98592010-02-16 11:11:14 +0000688 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
690 "Unknown struct or array constant!");
691
Owen Andersone50ed302009-08-10 22:56:29 +0000692 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000693 ComputeValueVTs(TLI, C->getType(), ValueVTs);
694 unsigned NumElts = ValueVTs.size();
695 if (NumElts == 0)
696 return SDValue(); // empty struct
697 SmallVector<SDValue, 4> Constants(NumElts);
698 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000699 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000700 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000701 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000702 else if (EltVT.isFloatingPoint())
703 Constants[i] = DAG.getConstantFP(0, EltVT);
704 else
705 Constants[i] = DAG.getConstant(0, EltVT);
706 }
Bill Wendling87710f02009-12-21 23:47:40 +0000707
Bill Wendling4533cac2010-01-28 21:51:40 +0000708 return DAG.getMergeValues(&Constants[0], NumElts,
709 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000710 }
711
Dan Gohman383b5f62010-04-17 15:32:28 +0000712 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000713 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000714
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000715 const VectorType *VecTy = cast<VectorType>(V->getType());
716 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000718 // Now that we know the number and type of the elements, get that number of
719 // elements into the Ops array based on what kind of constant it is.
720 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000721 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000722 for (unsigned i = 0; i != NumElements; ++i)
723 Ops.push_back(getValue(CP->getOperand(i)));
724 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000725 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000726 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000727
728 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000729 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000730 Op = DAG.getConstantFP(0, EltVT);
731 else
732 Op = DAG.getConstant(0, EltVT);
733 Ops.assign(NumElements, Op);
734 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000735
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000736 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000737 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
738 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000739 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000740
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000741 // If this is a static alloca, generate it as the frameindex instead of
742 // computation.
743 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
744 DenseMap<const AllocaInst*, int>::iterator SI =
745 FuncInfo.StaticAllocaMap.find(AI);
746 if (SI != FuncInfo.StaticAllocaMap.end())
747 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
748 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000750 unsigned InReg = FuncInfo.ValueMap[V];
751 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000752
Owen Anderson23b9b192009-08-12 00:36:31 +0000753 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000754 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000755 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000756}
757
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000758/// Get the EVTs and ArgFlags collections that represent the legalized return
759/// type of the given function. This does not require a DAG or a return value,
760/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000761static void getReturnInfo(const Type* ReturnType,
762 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000763 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000764 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000765 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000766 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000767 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000768 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000769 if (NumValues == 0) return;
770 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000771
772 for (unsigned j = 0, f = NumValues; j != f; ++j) {
773 EVT VT = ValueVTs[j];
774 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000775
776 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000777 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000778 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000779 ExtendKind = ISD::ZERO_EXTEND;
780
781 // FIXME: C calling convention requires the return type to be promoted to
782 // at least 32-bit. But this is not necessary for non-C calling
783 // conventions. The frontend should mark functions whose return values
784 // require promoting with signext or zeroext attributes.
785 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000786 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000787 if (VT.bitsLT(MinVT))
788 VT = MinVT;
789 }
790
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000791 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
792 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000793 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
794 PartVT.getTypeForEVT(ReturnType->getContext()));
795
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000796 // 'inreg' on function refers to return value
797 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000798 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000799 Flags.setInReg();
800
801 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000802 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000803 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000804 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000805 Flags.setZExt();
806
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000807 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000808 OutVTs.push_back(PartVT);
809 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000810 if (Offsets)
811 {
812 Offsets->push_back(Offset);
813 Offset += PartSize;
814 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000815 }
816 }
817}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000818
Dan Gohman46510a72010-04-15 01:51:59 +0000819void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000820 SDValue Chain = getControlRoot();
821 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000822 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000823
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000824 if (!FLI.CanLowerReturn) {
825 unsigned DemoteReg = FLI.DemoteRegister;
826 const Function *F = I.getParent()->getParent();
827
828 // Emit a store of the return value through the virtual register.
829 // Leave Outs empty so that LowerReturn won't try to load return
830 // registers the usual way.
831 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000832 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000833 PtrValueVTs);
834
835 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
836 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000837
Owen Andersone50ed302009-08-10 22:56:29 +0000838 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000839 SmallVector<uint64_t, 4> Offsets;
840 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000841 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000842
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000843 SmallVector<SDValue, 4> Chains(NumValues);
844 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000845 for (unsigned i = 0; i != NumValues; ++i) {
846 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
847 DAG.getConstant(Offsets[i], PtrVT));
848 Chains[i] =
849 DAG.getStore(Chain, getCurDebugLoc(),
850 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000851 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000852 }
853
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000854 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
855 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000856 } else if (I.getNumOperands() != 0) {
857 SmallVector<EVT, 4> ValueVTs;
858 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
859 unsigned NumValues = ValueVTs.size();
860 if (NumValues) {
861 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000862 for (unsigned j = 0, f = NumValues; j != f; ++j) {
863 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000864
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000865 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000866
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000867 const Function *F = I.getParent()->getParent();
868 if (F->paramHasAttr(0, Attribute::SExt))
869 ExtendKind = ISD::SIGN_EXTEND;
870 else if (F->paramHasAttr(0, Attribute::ZExt))
871 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000872
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000873 // FIXME: C calling convention requires the return type to be promoted
874 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000875 // conventions. The frontend should mark functions whose return values
876 // require promoting with signext or zeroext attributes.
877 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
878 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
879 if (VT.bitsLT(MinVT))
880 VT = MinVT;
881 }
882
883 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
884 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
885 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000886 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000887 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
888 &Parts[0], NumParts, PartVT, ExtendKind);
889
890 // 'inreg' on function refers to return value
891 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
892 if (F->paramHasAttr(0, Attribute::InReg))
893 Flags.setInReg();
894
895 // Propagate extension type if any
896 if (F->paramHasAttr(0, Attribute::SExt))
897 Flags.setSExt();
898 else if (F->paramHasAttr(0, Attribute::ZExt))
899 Flags.setZExt();
900
901 for (unsigned i = 0; i < NumParts; ++i)
902 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000903 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 }
905 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000906
907 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000908 CallingConv::ID CallConv =
909 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000910 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
911 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000912
913 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000915 "LowerReturn didn't return a valid chain!");
916
917 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000918 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000919}
920
Dan Gohmanad62f532009-04-23 23:13:24 +0000921/// CopyToExportRegsIfNeeded - If the given value has virtual registers
922/// created for it, emit nodes to copy the value into the virtual
923/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000924void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000925 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
926 if (VMI != FuncInfo.ValueMap.end()) {
927 assert(!V->use_empty() && "Unused value assigned virtual registers!");
928 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000929 }
930}
931
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000932/// ExportFromCurrentBlock - If this condition isn't known to be exported from
933/// the current basic block, add it to ValueMap now so that we'll get a
934/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000935void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000936 // No need to export constants.
937 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000938
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000939 // Already exported?
940 if (FuncInfo.isExportedInst(V)) return;
941
942 unsigned Reg = FuncInfo.InitializeRegForValue(V);
943 CopyValueToVirtualRegister(V, Reg);
944}
945
Dan Gohman46510a72010-04-15 01:51:59 +0000946bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000947 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000948 // The operands of the setcc have to be in this block. We don't know
949 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000950 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000951 // Can export from current BB.
952 if (VI->getParent() == FromBB)
953 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000954
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000955 // Is already exported, noop.
956 return FuncInfo.isExportedInst(V);
957 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000959 // If this is an argument, we can export it if the BB is the entry block or
960 // if it is already exported.
961 if (isa<Argument>(V)) {
962 if (FromBB == &FromBB->getParent()->getEntryBlock())
963 return true;
964
965 // Otherwise, can only export this if it is already exported.
966 return FuncInfo.isExportedInst(V);
967 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000969 // Otherwise, constants can always be exported.
970 return true;
971}
972
973static bool InBlock(const Value *V, const BasicBlock *BB) {
974 if (const Instruction *I = dyn_cast<Instruction>(V))
975 return I->getParent() == BB;
976 return true;
977}
978
Dan Gohmanc2277342008-10-17 21:16:08 +0000979/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
980/// This function emits a branch and is used at the leaves of an OR or an
981/// AND operator tree.
982///
983void
Dan Gohman46510a72010-04-15 01:51:59 +0000984SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000985 MachineBasicBlock *TBB,
986 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000987 MachineBasicBlock *CurBB,
988 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000989 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000990
Dan Gohmanc2277342008-10-17 21:16:08 +0000991 // If the leaf of the tree is a comparison, merge the condition into
992 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +0000993 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000994 // The operands of the cmp have to be in this block. We don't know
995 // how to export them from some other block. If this is the first block
996 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +0000997 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +0000998 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
999 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001000 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001001 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001002 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001003 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001004 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 } else {
1006 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001007 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001009
1010 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001011 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1012 SwitchCases.push_back(CB);
1013 return;
1014 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001015 }
1016
1017 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001018 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001019 NULL, TBB, FBB, CurBB);
1020 SwitchCases.push_back(CB);
1021}
1022
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001023/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001024void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001025 MachineBasicBlock *TBB,
1026 MachineBasicBlock *FBB,
1027 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001028 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001029 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001030 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001031 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001032 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001033 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1034 BOp->getParent() != CurBB->getBasicBlock() ||
1035 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1036 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001037 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001038 return;
1039 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001041 // Create TmpBB after CurBB.
1042 MachineFunction::iterator BBI = CurBB;
1043 MachineFunction &MF = DAG.getMachineFunction();
1044 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1045 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001047 if (Opc == Instruction::Or) {
1048 // Codegen X | Y as:
1049 // jmp_if_X TBB
1050 // jmp TmpBB
1051 // TmpBB:
1052 // jmp_if_Y TBB
1053 // jmp FBB
1054 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001056 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001057 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001058
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001059 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001060 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001061 } else {
1062 assert(Opc == Instruction::And && "Unknown merge op!");
1063 // Codegen X & Y as:
1064 // jmp_if_X TmpBB
1065 // jmp FBB
1066 // TmpBB:
1067 // jmp_if_Y TBB
1068 // jmp FBB
1069 //
1070 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001071
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001072 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001073 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001075 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001076 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001077 }
1078}
1079
1080/// If the set of cases should be emitted as a series of branches, return true.
1081/// If we should emit this as a bunch of and/or'd together conditions, return
1082/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001083bool
Dan Gohman2048b852009-11-23 18:04:58 +00001084SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001085 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001086
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 // If this is two comparisons of the same values or'd or and'd together, they
1088 // will get folded into a single comparison, so don't emit two blocks.
1089 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1090 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1091 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1092 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1093 return false;
1094 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001095
Chris Lattner133ce872010-01-02 00:00:03 +00001096 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1097 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1098 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1099 Cases[0].CC == Cases[1].CC &&
1100 isa<Constant>(Cases[0].CmpRHS) &&
1101 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1102 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1103 return false;
1104 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1105 return false;
1106 }
1107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001108 return true;
1109}
1110
Dan Gohman46510a72010-04-15 01:51:59 +00001111void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001112 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001114 // Update machine-CFG edges.
1115 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1116
1117 // Figure out which block is immediately after the current one.
1118 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001119 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001120 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001121 NextBlock = BBI;
1122
1123 if (I.isUnconditional()) {
1124 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001125 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001126
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001127 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001128 if (Succ0MBB != NextBlock)
1129 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001130 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001131 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001132
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001133 return;
1134 }
1135
1136 // If this condition is one of the special cases we handle, do special stuff
1137 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001138 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001139 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1140
1141 // If this is a series of conditions that are or'd or and'd together, emit
1142 // this as a sequence of branches instead of setcc's with and/or operations.
1143 // For example, instead of something like:
1144 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001145 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001147 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001148 // or C, F
1149 // jnz foo
1150 // Emit:
1151 // cmp A, B
1152 // je foo
1153 // cmp D, E
1154 // jle foo
1155 //
Dan Gohman46510a72010-04-15 01:51:59 +00001156 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 (BOp->getOpcode() == Instruction::And ||
1159 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001160 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1161 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001162 // If the compares in later blocks need to use values not currently
1163 // exported from this block, export them now. This block should always
1164 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001165 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 // Allow some cases to be rejected.
1168 if (ShouldEmitAsBranches(SwitchCases)) {
1169 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1170 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1171 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1172 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001175 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 SwitchCases.erase(SwitchCases.begin());
1177 return;
1178 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001180 // Okay, we decided not to do this, remove any inserted MBB's and clear
1181 // SwitchCases.
1182 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001183 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 SwitchCases.clear();
1186 }
1187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001189 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001190 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001191 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 // Use visitSwitchCase to actually insert the fast branch sequence for this
1194 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001195 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196}
1197
1198/// visitSwitchCase - Emits the necessary code to represent a single node in
1199/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001200void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1201 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202 SDValue Cond;
1203 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001204 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001205
1206 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 if (CB.CmpMHS == NULL) {
1208 // Fold "(X == true)" to X and "(X == false)" to !X to
1209 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001210 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001211 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001213 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001214 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001216 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001217 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 } else {
1220 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1221
Anton Korobeynikov23218582008-12-23 22:25:27 +00001222 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1223 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224
1225 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001226 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227
1228 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001230 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001232 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001233 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001235 DAG.getConstant(High-Low, VT), ISD::SETULE);
1236 }
1237 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001240 SwitchBB->addSuccessor(CB.TrueBB);
1241 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001243 // Set NextBlock to be the MBB immediately after the current one, if any.
1244 // This is used to avoid emitting unnecessary branches to the next block.
1245 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001246 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001247 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001249
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001250 // If the lhs block is the next block, invert the condition so that we can
1251 // fall through to the lhs instead of the rhs block.
1252 if (CB.TrueBB == NextBlock) {
1253 std::swap(CB.TrueBB, CB.FalseBB);
1254 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001255 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001257
Dale Johannesenf5d97892009-02-04 01:48:28 +00001258 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001259 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001260 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 // If the branch was constant folded, fix up the CFG.
1263 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001264 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001265 } else {
1266 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001267 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001268 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001269
Bill Wendling4533cac2010-01-28 21:51:40 +00001270 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001271 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1272 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001273 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001274
1275 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001276}
1277
1278/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001279void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001280 // Emit the code for the jump table
1281 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001282 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001283 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1284 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001285 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001286 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1287 MVT::Other, Index.getValue(1),
1288 Table, Index);
1289 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290}
1291
1292/// visitJumpTableHeader - This function emits necessary code to produce index
1293/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001294void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001295 JumpTableHeader &JTH,
1296 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001297 // Subtract the lowest switch case value from the value being switched on and
1298 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001299 // difference between smallest and largest cases.
1300 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001301 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001302 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001303 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001304
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001305 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001306 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001307 // can be used as an index into the jump table in a subsequent basic block.
1308 // This value may be smaller or larger than the target's pointer type, and
1309 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001310 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001313 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1314 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 JT.Reg = JumpTableReg;
1316
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001317 // Emit the range check for the jump table, and branch to the default block
1318 // for the switch statement if the value being switched on exceeds the largest
1319 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001320 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001321 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001322 DAG.getConstant(JTH.Last-JTH.First,VT),
1323 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324
1325 // Set NextBlock to be the MBB immediately after the current one, if any.
1326 // This is used to avoid emitting unnecessary branches to the next block.
1327 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001328 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001329
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001330 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 NextBlock = BBI;
1332
Dale Johannesen66978ee2009-01-31 02:22:37 +00001333 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001335 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336
Bill Wendling4533cac2010-01-28 21:51:40 +00001337 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001338 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1339 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001340
Bill Wendling87710f02009-12-21 23:47:40 +00001341 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342}
1343
1344/// visitBitTestHeader - This function emits necessary code to produce value
1345/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001346void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1347 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001348 // Subtract the minimum value
1349 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001350 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001351 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001352 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353
1354 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001355 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001356 TLI.getSetCCResultType(Sub.getValueType()),
1357 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001358 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359
Bill Wendling87710f02009-12-21 23:47:40 +00001360 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1361 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362
Duncan Sands92abc622009-01-31 15:50:11 +00001363 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001364 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1365 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001366
1367 // Set NextBlock to be the MBB immediately after the current one, if any.
1368 // This is used to avoid emitting unnecessary branches to the next block.
1369 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001370 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001371 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001372 NextBlock = BBI;
1373
1374 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1375
Dan Gohman99be8ae2010-04-19 22:41:47 +00001376 SwitchBB->addSuccessor(B.Default);
1377 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378
Dale Johannesen66978ee2009-01-31 02:22:37 +00001379 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001380 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001381 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001382
Bill Wendling4533cac2010-01-28 21:51:40 +00001383 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001384 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1385 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001386
Bill Wendling87710f02009-12-21 23:47:40 +00001387 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001388}
1389
1390/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001391void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1392 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001393 BitTestCase &B,
1394 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001395 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001396 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001397 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001398 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001399 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001400 DAG.getConstant(1, TLI.getPointerTy()),
1401 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001402
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001403 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001404 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001405 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001406 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001407 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1408 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001409 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001410 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001411
Dan Gohman99be8ae2010-04-19 22:41:47 +00001412 SwitchBB->addSuccessor(B.TargetBB);
1413 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001414
Dale Johannesen66978ee2009-01-31 02:22:37 +00001415 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001417 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418
1419 // Set NextBlock to be the MBB immediately after the current one, if any.
1420 // This is used to avoid emitting unnecessary branches to the next block.
1421 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001422 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001423 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001424 NextBlock = BBI;
1425
Bill Wendling4533cac2010-01-28 21:51:40 +00001426 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001427 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1428 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001429
Bill Wendling87710f02009-12-21 23:47:40 +00001430 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001431}
1432
Dan Gohman46510a72010-04-15 01:51:59 +00001433void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001434 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 // Retrieve successors.
1437 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1438 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1439
Gabor Greifb67e6b32009-01-15 11:10:44 +00001440 const Value *Callee(I.getCalledValue());
1441 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 visitInlineAsm(&I);
1443 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001444 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445
1446 // If the value of the invoke is used outside of its defining block, make it
1447 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001448 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449
1450 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001451 InvokeMBB->addSuccessor(Return);
1452 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453
1454 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001455 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1456 MVT::Other, getControlRoot(),
1457 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458}
1459
Dan Gohman46510a72010-04-15 01:51:59 +00001460void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001461}
1462
1463/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1464/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001465bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1466 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001467 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001468 MachineBasicBlock *Default,
1469 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001471
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001472 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001473 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001474 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001475 return false;
1476
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001477 // Get the MachineFunction which holds the current MBB. This is used when
1478 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001479 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480
1481 // Figure out which block is immediately after the current one.
1482 MachineBasicBlock *NextBlock = 0;
1483 MachineFunction::iterator BBI = CR.CaseBB;
1484
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001485 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486 NextBlock = BBI;
1487
1488 // TODO: If any two of the cases has the same destination, and if one value
1489 // is the same as the other, but has one bit unset that the other has set,
1490 // use bit manipulation to do two compares at once. For example:
1491 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001493 // Rearrange the case blocks so that the last one falls through if possible.
1494 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1495 // The last case block won't fall through into 'NextBlock' if we emit the
1496 // branches in this order. See if rearranging a case value would help.
1497 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1498 if (I->BB == NextBlock) {
1499 std::swap(*I, BackCase);
1500 break;
1501 }
1502 }
1503 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001505 // Create a CaseBlock record representing a conditional branch to
1506 // the Case's target mbb if the value being switched on SV is equal
1507 // to C.
1508 MachineBasicBlock *CurBlock = CR.CaseBB;
1509 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1510 MachineBasicBlock *FallThrough;
1511 if (I != E-1) {
1512 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1513 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001514
1515 // Put SV in a virtual register to make it available from the new blocks.
1516 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001517 } else {
1518 // If the last case doesn't match, go to the default block.
1519 FallThrough = Default;
1520 }
1521
Dan Gohman46510a72010-04-15 01:51:59 +00001522 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001523 ISD::CondCode CC;
1524 if (I->High == I->Low) {
1525 // This is just small small case range :) containing exactly 1 case
1526 CC = ISD::SETEQ;
1527 LHS = SV; RHS = I->High; MHS = NULL;
1528 } else {
1529 CC = ISD::SETLE;
1530 LHS = I->Low; MHS = SV; RHS = I->High;
1531 }
1532 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001533
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534 // If emitting the first comparison, just call visitSwitchCase to emit the
1535 // code into the current block. Otherwise, push the CaseBlock onto the
1536 // vector to be later processed by SDISel, and insert the node's MBB
1537 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001538 if (CurBlock == SwitchBB)
1539 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001540 else
1541 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001542
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543 CurBlock = FallThrough;
1544 }
1545
1546 return true;
1547}
1548
1549static inline bool areJTsAllowed(const TargetLowering &TLI) {
1550 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1552 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001553}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001554
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001555static APInt ComputeRange(const APInt &First, const APInt &Last) {
1556 APInt LastExt(Last), FirstExt(First);
1557 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1558 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1559 return (LastExt - FirstExt + 1ULL);
1560}
1561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001563bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1564 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001565 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001566 MachineBasicBlock* Default,
1567 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568 Case& FrontCase = *CR.Range.first;
1569 Case& BackCase = *(CR.Range.second-1);
1570
Chris Lattnere880efe2009-11-07 07:50:34 +00001571 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1572 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001573
Chris Lattnere880efe2009-11-07 07:50:34 +00001574 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001575 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1576 I!=E; ++I)
1577 TSize += I->size();
1578
Dan Gohmane0567812010-04-08 23:03:40 +00001579 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001581
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001582 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001583 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001584 if (Density < 0.4)
1585 return false;
1586
David Greene4b69d992010-01-05 01:24:57 +00001587 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001588 << "First entry: " << First << ". Last entry: " << Last << '\n'
1589 << "Range: " << Range
1590 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591
1592 // Get the MachineFunction which holds the current MBB. This is used when
1593 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001594 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595
1596 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001597 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001598 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001599
1600 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1601
1602 // Create a new basic block to hold the code for loading the address
1603 // of the jump table, and jumping to it. Update successor information;
1604 // we will either branch to the default case for the switch, or the jump
1605 // table.
1606 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1607 CurMF->insert(BBI, JumpTableBB);
1608 CR.CaseBB->addSuccessor(Default);
1609 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001610
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001611 // Build a vector of destination BBs, corresponding to each target
1612 // of the jump table. If the value of the jump table slot corresponds to
1613 // a case statement, push the case's BB onto the vector, otherwise, push
1614 // the default BB.
1615 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001616 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001618 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1619 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001620
1621 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 DestBBs.push_back(I->BB);
1623 if (TEI==High)
1624 ++I;
1625 } else {
1626 DestBBs.push_back(Default);
1627 }
1628 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001629
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001630 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001631 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1632 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633 E = DestBBs.end(); I != E; ++I) {
1634 if (!SuccsHandled[(*I)->getNumber()]) {
1635 SuccsHandled[(*I)->getNumber()] = true;
1636 JumpTableBB->addSuccessor(*I);
1637 }
1638 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001640 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001641 unsigned JTEncoding = TLI.getJumpTableEncoding();
1642 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001643 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001644
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001645 // Set the jump table information so that we can codegen it as a second
1646 // MachineBasicBlock
1647 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001648 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1649 if (CR.CaseBB == SwitchBB)
1650 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001652 JTCases.push_back(JumpTableBlock(JTH, JT));
1653
1654 return true;
1655}
1656
1657/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1658/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001659bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1660 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001661 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001662 MachineBasicBlock *Default,
1663 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001664 // Get the MachineFunction which holds the current MBB. This is used when
1665 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001666 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001667
1668 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001669 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001670 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001671
1672 Case& FrontCase = *CR.Range.first;
1673 Case& BackCase = *(CR.Range.second-1);
1674 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1675
1676 // Size is the number of Cases represented by this range.
1677 unsigned Size = CR.Range.second - CR.Range.first;
1678
Chris Lattnere880efe2009-11-07 07:50:34 +00001679 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1680 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 double FMetric = 0;
1682 CaseItr Pivot = CR.Range.first + Size/2;
1683
1684 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1685 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001686 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001687 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1688 I!=E; ++I)
1689 TSize += I->size();
1690
Chris Lattnere880efe2009-11-07 07:50:34 +00001691 APInt LSize = FrontCase.size();
1692 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001693 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001694 << "First: " << First << ", Last: " << Last <<'\n'
1695 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1697 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001698 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1699 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001700 APInt Range = ComputeRange(LEnd, RBegin);
1701 assert((Range - 2ULL).isNonNegative() &&
1702 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001703 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001704 (LEnd - First + 1ULL).roundToDouble();
1705 double RDensity = (double)RSize.roundToDouble() /
1706 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001707 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001709 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001710 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1711 << "LDensity: " << LDensity
1712 << ", RDensity: " << RDensity << '\n'
1713 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 if (FMetric < Metric) {
1715 Pivot = J;
1716 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001717 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001718 }
1719
1720 LSize += J->size();
1721 RSize -= J->size();
1722 }
1723 if (areJTsAllowed(TLI)) {
1724 // If our case is dense we *really* should handle it earlier!
1725 assert((FMetric > 0) && "Should handle dense range earlier!");
1726 } else {
1727 Pivot = CR.Range.first + Size/2;
1728 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001729
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 CaseRange LHSR(CR.Range.first, Pivot);
1731 CaseRange RHSR(Pivot, CR.Range.second);
1732 Constant *C = Pivot->Low;
1733 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001734
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001736 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001738 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739 // Pivot's Value, then we can branch directly to the LHS's Target,
1740 // rather than creating a leaf node for it.
1741 if ((LHSR.second - LHSR.first) == 1 &&
1742 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001743 cast<ConstantInt>(C)->getValue() ==
1744 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745 TrueBB = LHSR.first->BB;
1746 } else {
1747 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1748 CurMF->insert(BBI, TrueBB);
1749 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001750
1751 // Put SV in a virtual register to make it available from the new blocks.
1752 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001753 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001755 // Similar to the optimization above, if the Value being switched on is
1756 // known to be less than the Constant CR.LT, and the current Case Value
1757 // is CR.LT - 1, then we can branch directly to the target block for
1758 // the current Case Value, rather than emitting a RHS leaf node for it.
1759 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001760 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1761 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001762 FalseBB = RHSR.first->BB;
1763 } else {
1764 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1765 CurMF->insert(BBI, FalseBB);
1766 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001767
1768 // Put SV in a virtual register to make it available from the new blocks.
1769 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770 }
1771
1772 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001773 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001774 // Otherwise, branch to LHS.
1775 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1776
Dan Gohman99be8ae2010-04-19 22:41:47 +00001777 if (CR.CaseBB == SwitchBB)
1778 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779 else
1780 SwitchCases.push_back(CB);
1781
1782 return true;
1783}
1784
1785/// handleBitTestsSwitchCase - if current case range has few destination and
1786/// range span less, than machine word bitwidth, encode case range into series
1787/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001788bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1789 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001790 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001791 MachineBasicBlock* Default,
1792 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001793 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001794 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001795
1796 Case& FrontCase = *CR.Range.first;
1797 Case& BackCase = *(CR.Range.second-1);
1798
1799 // Get the MachineFunction which holds the current MBB. This is used when
1800 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001801 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001802
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001803 // If target does not have legal shift left, do not emit bit tests at all.
1804 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1805 return false;
1806
Anton Korobeynikov23218582008-12-23 22:25:27 +00001807 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1809 I!=E; ++I) {
1810 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001811 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814 // Count unique destinations
1815 SmallSet<MachineBasicBlock*, 4> Dests;
1816 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1817 Dests.insert(I->BB);
1818 if (Dests.size() > 3)
1819 // Don't bother the code below, if there are too much unique destinations
1820 return false;
1821 }
David Greene4b69d992010-01-05 01:24:57 +00001822 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001823 << Dests.size() << '\n'
1824 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001827 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1828 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001829 APInt cmpRange = maxValue - minValue;
1830
David Greene4b69d992010-01-05 01:24:57 +00001831 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001832 << "Low bound: " << minValue << '\n'
1833 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001834
Dan Gohmane0567812010-04-08 23:03:40 +00001835 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001836 (!(Dests.size() == 1 && numCmps >= 3) &&
1837 !(Dests.size() == 2 && numCmps >= 5) &&
1838 !(Dests.size() >= 3 && numCmps >= 6)))
1839 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001840
David Greene4b69d992010-01-05 01:24:57 +00001841 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001842 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 // Optimize the case where all the case values fit in a
1845 // word without having to subtract minValue. In this case,
1846 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001847 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001848 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001849 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001850 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001851 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001853 CaseBitsVector CasesBits;
1854 unsigned i, count = 0;
1855
1856 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1857 MachineBasicBlock* Dest = I->BB;
1858 for (i = 0; i < count; ++i)
1859 if (Dest == CasesBits[i].BB)
1860 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001861
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001862 if (i == count) {
1863 assert((count < 3) && "Too much destinations to test!");
1864 CasesBits.push_back(CaseBits(0, Dest, 0));
1865 count++;
1866 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001867
1868 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1869 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1870
1871 uint64_t lo = (lowValue - lowBound).getZExtValue();
1872 uint64_t hi = (highValue - lowBound).getZExtValue();
1873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874 for (uint64_t j = lo; j <= hi; j++) {
1875 CasesBits[i].Mask |= 1ULL << j;
1876 CasesBits[i].Bits++;
1877 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001879 }
1880 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001881
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 BitTestInfo BTC;
1883
1884 // Figure out which block is immediately after the current one.
1885 MachineFunction::iterator BBI = CR.CaseBB;
1886 ++BBI;
1887
1888 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1889
David Greene4b69d992010-01-05 01:24:57 +00001890 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001892 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001893 << ", Bits: " << CasesBits[i].Bits
1894 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001895
1896 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1897 CurMF->insert(BBI, CaseBB);
1898 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1899 CaseBB,
1900 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001901
1902 // Put SV in a virtual register to make it available from the new blocks.
1903 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001904 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905
1906 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001907 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 CR.CaseBB, Default, BTC);
1909
Dan Gohman99be8ae2010-04-19 22:41:47 +00001910 if (CR.CaseBB == SwitchBB)
1911 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001913 BitTestCases.push_back(BTB);
1914
1915 return true;
1916}
1917
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001919size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1920 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001921 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001922
1923 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1926 Cases.push_back(Case(SI.getSuccessorValue(i),
1927 SI.getSuccessorValue(i),
1928 SMBB));
1929 }
1930 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1931
1932 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 // Must recompute end() each iteration because it may be
1935 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001936 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1937 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1938 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001939 MachineBasicBlock* nextBB = J->BB;
1940 MachineBasicBlock* currentBB = I->BB;
1941
1942 // If the two neighboring cases go to the same destination, merge them
1943 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001944 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 I->High = J->High;
1946 J = Cases.erase(J);
1947 } else {
1948 I = J++;
1949 }
1950 }
1951
1952 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1953 if (I->Low != I->High)
1954 // A range counts double, since it requires two compares.
1955 ++numCmps;
1956 }
1957
1958 return numCmps;
1959}
1960
Dan Gohman46510a72010-04-15 01:51:59 +00001961void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001962 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1963
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001964 // Figure out which block is immediately after the current one.
1965 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1967
1968 // If there is only the default destination, branch to it if it is not the
1969 // next basic block. Otherwise, just fall through.
1970 if (SI.getNumOperands() == 2) {
1971 // Update machine-CFG edges.
1972
1973 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001974 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001975 if (Default != NextBlock)
1976 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1977 MVT::Other, getControlRoot(),
1978 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001980 return;
1981 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001983 // If there are any non-default case statements, create a vector of Cases
1984 // representing each one, and sort the vector so that we can efficiently
1985 // create a binary search tree from them.
1986 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001987 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001988 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001989 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001990 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001991
1992 // Get the Value to be switched on and default basic blocks, which will be
1993 // inserted into CaseBlock records, representing basic blocks in the binary
1994 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00001995 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001996
1997 // Push the initial CaseRec onto the worklist
1998 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001999 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2000 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002001
2002 while (!WorkList.empty()) {
2003 // Grab a record representing a case range to process off the worklist
2004 CaseRec CR = WorkList.back();
2005 WorkList.pop_back();
2006
Dan Gohman99be8ae2010-04-19 22:41:47 +00002007 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002009
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002010 // If the range has few cases (two or less) emit a series of specific
2011 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002012 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002015 // If the switch has more than 5 blocks, and at least 40% dense, and the
2016 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002018 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2022 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002023 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002024 }
2025}
2026
Dan Gohman46510a72010-04-15 01:51:59 +00002027void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002028 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2029
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002030 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002031 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002032 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002033 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002034 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002035 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002036 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2037 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002038 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002039
Bill Wendling4533cac2010-01-28 21:51:40 +00002040 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2041 MVT::Other, getControlRoot(),
2042 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002043}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002044
Dan Gohman46510a72010-04-15 01:51:59 +00002045void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002046 // -0.0 - X --> fneg
2047 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002048 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002049 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2050 const VectorType *DestTy = cast<VectorType>(I.getType());
2051 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002052 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002053 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002054 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002055 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002057 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2058 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059 return;
2060 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002061 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002063
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002064 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002065 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002066 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002067 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2068 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002069 return;
2070 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002071
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002072 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002073}
2074
Dan Gohman46510a72010-04-15 01:51:59 +00002075void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002076 SDValue Op1 = getValue(I.getOperand(0));
2077 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002078 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2079 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002080}
2081
Dan Gohman46510a72010-04-15 01:51:59 +00002082void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083 SDValue Op1 = getValue(I.getOperand(0));
2084 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002085 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002086 Op2.getValueType() != TLI.getShiftAmountTy()) {
2087 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002088 EVT PTy = TLI.getPointerTy();
2089 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002090 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002091 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2092 TLI.getShiftAmountTy(), Op2);
2093 // If the operand is larger than the shift count type but the shift
2094 // count type has enough bits to represent any shift value, truncate
2095 // it now. This is a common case and it exposes the truncate to
2096 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002097 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002098 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2099 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2100 TLI.getShiftAmountTy(), Op2);
2101 // Otherwise we'll need to temporarily settle for some other
2102 // convenient type; type legalization will make adjustments as
2103 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002104 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002105 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002106 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002107 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002108 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002109 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002110 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002111
Bill Wendling4533cac2010-01-28 21:51:40 +00002112 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2113 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114}
2115
Dan Gohman46510a72010-04-15 01:51:59 +00002116void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002117 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002118 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002120 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002121 predicate = ICmpInst::Predicate(IC->getPredicate());
2122 SDValue Op1 = getValue(I.getOperand(0));
2123 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002124 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002125
Owen Andersone50ed302009-08-10 22:56:29 +00002126 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002127 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128}
2129
Dan Gohman46510a72010-04-15 01:51:59 +00002130void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002131 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002132 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002134 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135 predicate = FCmpInst::Predicate(FC->getPredicate());
2136 SDValue Op1 = getValue(I.getOperand(0));
2137 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002138 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002139 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002140 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141}
2142
Dan Gohman46510a72010-04-15 01:51:59 +00002143void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002144 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002145 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2146 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002147 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002148
Bill Wendling49fcff82009-12-21 22:30:11 +00002149 SmallVector<SDValue, 4> Values(NumValues);
2150 SDValue Cond = getValue(I.getOperand(0));
2151 SDValue TrueVal = getValue(I.getOperand(1));
2152 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002153
Bill Wendling4533cac2010-01-28 21:51:40 +00002154 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002155 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002156 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2157 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002158 SDValue(TrueVal.getNode(),
2159 TrueVal.getResNo() + i),
2160 SDValue(FalseVal.getNode(),
2161 FalseVal.getResNo() + i));
2162
Bill Wendling4533cac2010-01-28 21:51:40 +00002163 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2164 DAG.getVTList(&ValueVTs[0], NumValues),
2165 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002166}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002167
Dan Gohman46510a72010-04-15 01:51:59 +00002168void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002169 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2170 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002171 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002172 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002173}
2174
Dan Gohman46510a72010-04-15 01:51:59 +00002175void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2177 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2178 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002179 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002180 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181}
2182
Dan Gohman46510a72010-04-15 01:51:59 +00002183void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002184 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2185 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2186 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002188 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189}
2190
Dan Gohman46510a72010-04-15 01:51:59 +00002191void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192 // FPTrunc is never a no-op cast, no need to check
2193 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002194 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002195 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2196 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197}
2198
Dan Gohman46510a72010-04-15 01:51:59 +00002199void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002200 // FPTrunc is never a no-op cast, no need to check
2201 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002203 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002204}
2205
Dan Gohman46510a72010-04-15 01:51:59 +00002206void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207 // FPToUI is never a no-op cast, no need to check
2208 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002209 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002210 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211}
2212
Dan Gohman46510a72010-04-15 01:51:59 +00002213void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002214 // FPToSI is never a no-op cast, no need to check
2215 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002216 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002217 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002218}
2219
Dan Gohman46510a72010-04-15 01:51:59 +00002220void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221 // UIToFP is never a no-op cast, no need to check
2222 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002223 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002224 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225}
2226
Dan Gohman46510a72010-04-15 01:51:59 +00002227void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002228 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002230 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002231 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232}
2233
Dan Gohman46510a72010-04-15 01:51:59 +00002234void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235 // What to do depends on the size of the integer and the size of the pointer.
2236 // We can either truncate, zero extend, or no-op, accordingly.
2237 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002238 EVT SrcVT = N.getValueType();
2239 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002240 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241}
2242
Dan Gohman46510a72010-04-15 01:51:59 +00002243void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244 // What to do depends on the size of the integer and the size of the pointer.
2245 // We can either truncate, zero extend, or no-op, accordingly.
2246 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002247 EVT SrcVT = N.getValueType();
2248 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002249 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002250}
2251
Dan Gohman46510a72010-04-15 01:51:59 +00002252void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002253 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002254 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002255
Bill Wendling49fcff82009-12-21 22:30:11 +00002256 // BitCast assures us that source and destination are the same size so this is
2257 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002258 if (DestVT != N.getValueType())
2259 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2260 DestVT, N)); // convert types.
2261 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002262 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002263}
2264
Dan Gohman46510a72010-04-15 01:51:59 +00002265void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266 SDValue InVec = getValue(I.getOperand(0));
2267 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002268 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002269 TLI.getPointerTy(),
2270 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002271 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2272 TLI.getValueType(I.getType()),
2273 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274}
2275
Dan Gohman46510a72010-04-15 01:51:59 +00002276void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002277 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002278 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002279 TLI.getPointerTy(),
2280 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002281 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2282 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283}
2284
Mon P Wangaeb06d22008-11-10 04:46:22 +00002285// Utility for visitShuffleVector - Returns true if the mask is mask starting
2286// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002287static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2288 unsigned MaskNumElts = Mask.size();
2289 for (unsigned i = 0; i != MaskNumElts; ++i)
2290 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002291 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002292 return true;
2293}
2294
Dan Gohman46510a72010-04-15 01:51:59 +00002295void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002296 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002297 SDValue Src1 = getValue(I.getOperand(0));
2298 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002299
Nate Begeman9008ca62009-04-27 18:41:29 +00002300 // Convert the ConstantVector mask operand into an array of ints, with -1
2301 // representing undef values.
2302 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002303 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002304 unsigned MaskNumElts = MaskElts.size();
2305 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002306 if (isa<UndefValue>(MaskElts[i]))
2307 Mask.push_back(-1);
2308 else
2309 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2310 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002311
Owen Andersone50ed302009-08-10 22:56:29 +00002312 EVT VT = TLI.getValueType(I.getType());
2313 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002314 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002315
Mon P Wangc7849c22008-11-16 05:06:27 +00002316 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002317 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2318 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002319 return;
2320 }
2321
2322 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002323 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2324 // Mask is longer than the source vectors and is a multiple of the source
2325 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002326 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002327 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2328 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002329 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2330 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002331 return;
2332 }
2333
Mon P Wangc7849c22008-11-16 05:06:27 +00002334 // Pad both vectors with undefs to make them the same length as the mask.
2335 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002336 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2337 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002338 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002339
Nate Begeman9008ca62009-04-27 18:41:29 +00002340 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2341 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002342 MOps1[0] = Src1;
2343 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002344
2345 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2346 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002347 &MOps1[0], NumConcat);
2348 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002349 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002350 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002351
Mon P Wangaeb06d22008-11-10 04:46:22 +00002352 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002353 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002354 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002355 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002356 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002357 MappedOps.push_back(Idx);
2358 else
2359 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002360 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002361
Bill Wendling4533cac2010-01-28 21:51:40 +00002362 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2363 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002364 return;
2365 }
2366
Mon P Wangc7849c22008-11-16 05:06:27 +00002367 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002368 // Analyze the access pattern of the vector to see if we can extract
2369 // two subvectors and do the shuffle. The analysis is done by calculating
2370 // the range of elements the mask access on both vectors.
2371 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2372 int MaxRange[2] = {-1, -1};
2373
Nate Begeman5a5ca152009-04-29 05:20:52 +00002374 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002375 int Idx = Mask[i];
2376 int Input = 0;
2377 if (Idx < 0)
2378 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002379
Nate Begeman5a5ca152009-04-29 05:20:52 +00002380 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002381 Input = 1;
2382 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002383 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002384 if (Idx > MaxRange[Input])
2385 MaxRange[Input] = Idx;
2386 if (Idx < MinRange[Input])
2387 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002388 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002389
Mon P Wangc7849c22008-11-16 05:06:27 +00002390 // Check if the access is smaller than the vector size and can we find
2391 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002392 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2393 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002394 int StartIdx[2]; // StartIdx to extract from
2395 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002396 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002397 RangeUse[Input] = 0; // Unused
2398 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002399 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002400 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002401 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002402 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002403 RangeUse[Input] = 1; // Extract from beginning of the vector
2404 StartIdx[Input] = 0;
2405 } else {
2406 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002407 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002408 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002409 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002410 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002411 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002412 }
2413
Bill Wendling636e2582009-08-21 18:16:06 +00002414 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002415 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002416 return;
2417 }
2418 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2419 // Extract appropriate subvector and generate a vector shuffle
2420 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002421 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002422 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002423 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002424 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002425 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002426 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002427 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002428
Mon P Wangc7849c22008-11-16 05:06:27 +00002429 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002430 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002431 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002432 int Idx = Mask[i];
2433 if (Idx < 0)
2434 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002435 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002436 MappedOps.push_back(Idx - StartIdx[0]);
2437 else
2438 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002439 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002440
Bill Wendling4533cac2010-01-28 21:51:40 +00002441 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2442 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002443 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002444 }
2445 }
2446
Mon P Wangc7849c22008-11-16 05:06:27 +00002447 // We can't use either concat vectors or extract subvectors so fall back to
2448 // replacing the shuffle with extract and build vector.
2449 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002450 EVT EltVT = VT.getVectorElementType();
2451 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002452 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002453 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002454 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002455 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002456 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002457 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002458 SDValue Res;
2459
Nate Begeman5a5ca152009-04-29 05:20:52 +00002460 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002461 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2462 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002463 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002464 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2465 EltVT, Src2,
2466 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2467
2468 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002469 }
2470 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002471
Bill Wendling4533cac2010-01-28 21:51:40 +00002472 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2473 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002474}
2475
Dan Gohman46510a72010-04-15 01:51:59 +00002476void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002477 const Value *Op0 = I.getOperand(0);
2478 const Value *Op1 = I.getOperand(1);
2479 const Type *AggTy = I.getType();
2480 const Type *ValTy = Op1->getType();
2481 bool IntoUndef = isa<UndefValue>(Op0);
2482 bool FromUndef = isa<UndefValue>(Op1);
2483
2484 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2485 I.idx_begin(), I.idx_end());
2486
Owen Andersone50ed302009-08-10 22:56:29 +00002487 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002488 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002489 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002490 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2491
2492 unsigned NumAggValues = AggValueVTs.size();
2493 unsigned NumValValues = ValValueVTs.size();
2494 SmallVector<SDValue, 4> Values(NumAggValues);
2495
2496 SDValue Agg = getValue(Op0);
2497 SDValue Val = getValue(Op1);
2498 unsigned i = 0;
2499 // Copy the beginning value(s) from the original aggregate.
2500 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002501 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002502 SDValue(Agg.getNode(), Agg.getResNo() + i);
2503 // Copy values from the inserted value(s).
2504 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002505 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002506 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2507 // Copy remaining value(s) from the original aggregate.
2508 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002509 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002510 SDValue(Agg.getNode(), Agg.getResNo() + i);
2511
Bill Wendling4533cac2010-01-28 21:51:40 +00002512 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2513 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2514 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002515}
2516
Dan Gohman46510a72010-04-15 01:51:59 +00002517void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002518 const Value *Op0 = I.getOperand(0);
2519 const Type *AggTy = Op0->getType();
2520 const Type *ValTy = I.getType();
2521 bool OutOfUndef = isa<UndefValue>(Op0);
2522
2523 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2524 I.idx_begin(), I.idx_end());
2525
Owen Andersone50ed302009-08-10 22:56:29 +00002526 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002527 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2528
2529 unsigned NumValValues = ValValueVTs.size();
2530 SmallVector<SDValue, 4> Values(NumValValues);
2531
2532 SDValue Agg = getValue(Op0);
2533 // Copy out the selected value(s).
2534 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2535 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002536 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002537 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002538 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539
Bill Wendling4533cac2010-01-28 21:51:40 +00002540 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2541 DAG.getVTList(&ValValueVTs[0], NumValValues),
2542 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002543}
2544
Dan Gohman46510a72010-04-15 01:51:59 +00002545void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002546 SDValue N = getValue(I.getOperand(0));
2547 const Type *Ty = I.getOperand(0)->getType();
2548
Dan Gohman46510a72010-04-15 01:51:59 +00002549 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002550 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002551 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002552 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2553 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2554 if (Field) {
2555 // N = N + Offset
2556 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002557 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 DAG.getIntPtrConstant(Offset));
2559 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002561 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002562 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2563 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2564
2565 // Offset canonically 0 for unions, but type changes
2566 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002567 } else {
2568 Ty = cast<SequentialType>(Ty)->getElementType();
2569
2570 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002571 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002572 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002573 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002574 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002575 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002576 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002577 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002578 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002579 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2580 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002581 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002582 else
Evan Chengb1032a82009-02-09 20:54:38 +00002583 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002584
Dale Johannesen66978ee2009-01-31 02:22:37 +00002585 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002586 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002587 continue;
2588 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002589
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002590 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002591 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2592 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002593 SDValue IdxN = getValue(Idx);
2594
2595 // If the index is smaller or larger than intptr_t, truncate or extend
2596 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002597 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002598
2599 // If this is a multiply by a power of two, turn it into a shl
2600 // immediately. This is a very common case.
2601 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002602 if (ElementSize.isPowerOf2()) {
2603 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002604 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002605 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002606 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002607 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002608 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002609 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002610 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002611 }
2612 }
2613
Scott Michelfdc40a02009-02-17 22:15:04 +00002614 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002615 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002616 }
2617 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002619 setValue(&I, N);
2620}
2621
Dan Gohman46510a72010-04-15 01:51:59 +00002622void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002623 // If this is a fixed sized alloca in the entry block of the function,
2624 // allocate it statically on the stack.
2625 if (FuncInfo.StaticAllocaMap.count(&I))
2626 return; // getValue will auto-populate this.
2627
2628 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002629 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002630 unsigned Align =
2631 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2632 I.getAlignment());
2633
2634 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002635
Chris Lattner0b18e592009-03-17 19:36:00 +00002636 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2637 AllocSize,
2638 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002639
Owen Andersone50ed302009-08-10 22:56:29 +00002640 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002641 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002643 // Handle alignment. If the requested alignment is less than or equal to
2644 // the stack alignment, ignore it. If the size is greater than or equal to
2645 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002646 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002647 if (Align <= StackAlign)
2648 Align = 0;
2649
2650 // Round the size of the allocation up to the stack alignment size
2651 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002652 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002653 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002654 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002656 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002657 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002658 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2660
2661 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002663 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002664 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002665 setValue(&I, DSA);
2666 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002668 // Inform the Frame Information that we have just allocated a variable-sized
2669 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002670 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002671}
2672
Dan Gohman46510a72010-04-15 01:51:59 +00002673void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002674 const Value *SV = I.getOperand(0);
2675 SDValue Ptr = getValue(SV);
2676
2677 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002679 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002680 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002681 unsigned Alignment = I.getAlignment();
2682
Owen Andersone50ed302009-08-10 22:56:29 +00002683 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002684 SmallVector<uint64_t, 4> Offsets;
2685 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2686 unsigned NumValues = ValueVTs.size();
2687 if (NumValues == 0)
2688 return;
2689
2690 SDValue Root;
2691 bool ConstantMemory = false;
2692 if (I.isVolatile())
2693 // Serialize volatile loads with other side effects.
2694 Root = getRoot();
2695 else if (AA->pointsToConstantMemory(SV)) {
2696 // Do not serialize (non-volatile) loads of constant memory with anything.
2697 Root = DAG.getEntryNode();
2698 ConstantMemory = true;
2699 } else {
2700 // Do not serialize non-volatile loads against each other.
2701 Root = DAG.getRoot();
2702 }
2703
2704 SmallVector<SDValue, 4> Values(NumValues);
2705 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002706 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002707 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002708 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2709 PtrVT, Ptr,
2710 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002711 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002712 A, SV, Offsets[i], isVolatile,
2713 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002714
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002715 Values[i] = L;
2716 Chains[i] = L.getValue(1);
2717 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002719 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002720 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002721 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002722 if (isVolatile)
2723 DAG.setRoot(Chain);
2724 else
2725 PendingLoads.push_back(Chain);
2726 }
2727
Bill Wendling4533cac2010-01-28 21:51:40 +00002728 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2729 DAG.getVTList(&ValueVTs[0], NumValues),
2730 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002731}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732
Dan Gohman46510a72010-04-15 01:51:59 +00002733void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2734 const Value *SrcV = I.getOperand(0);
2735 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736
Owen Andersone50ed302009-08-10 22:56:29 +00002737 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002738 SmallVector<uint64_t, 4> Offsets;
2739 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2740 unsigned NumValues = ValueVTs.size();
2741 if (NumValues == 0)
2742 return;
2743
2744 // Get the lowered operands. Note that we do this after
2745 // checking if NumResults is zero, because with zero results
2746 // the operands won't have values in the map.
2747 SDValue Src = getValue(SrcV);
2748 SDValue Ptr = getValue(PtrV);
2749
2750 SDValue Root = getRoot();
2751 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002752 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002753 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002754 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002755 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002756
2757 for (unsigned i = 0; i != NumValues; ++i) {
2758 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2759 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002760 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002761 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002762 Add, PtrV, Offsets[i], isVolatile,
2763 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002764 }
2765
Bill Wendling4533cac2010-01-28 21:51:40 +00002766 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2767 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002768}
2769
2770/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2771/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002772void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002773 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002774 bool HasChain = !I.doesNotAccessMemory();
2775 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2776
2777 // Build the operand list.
2778 SmallVector<SDValue, 8> Ops;
2779 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2780 if (OnlyLoad) {
2781 // We don't need to serialize loads against other loads.
2782 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002783 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784 Ops.push_back(getRoot());
2785 }
2786 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002787
2788 // Info is set by getTgtMemInstrinsic
2789 TargetLowering::IntrinsicInfo Info;
2790 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2791
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002792 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002793 if (!IsTgtIntrinsic)
2794 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795
2796 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002797 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002798 SDValue Op = getValue(I.getOperand(i));
2799 assert(TLI.isTypeLegal(Op.getValueType()) &&
2800 "Intrinsic uses a non-legal type?");
2801 Ops.push_back(Op);
2802 }
2803
Owen Andersone50ed302009-08-10 22:56:29 +00002804 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002805 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2806#ifndef NDEBUG
2807 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2808 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2809 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 }
Bob Wilson8d919552009-07-31 22:41:21 +00002811#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002812
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002814 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002815
Bob Wilson8d919552009-07-31 22:41:21 +00002816 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817
2818 // Create the node.
2819 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002820 if (IsTgtIntrinsic) {
2821 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002822 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002823 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002824 Info.memVT, Info.ptrVal, Info.offset,
2825 Info.align, Info.vol,
2826 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002827 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002828 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002829 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002830 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002831 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002832 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002833 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002834 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002835 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002836 }
2837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002838 if (HasChain) {
2839 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2840 if (OnlyLoad)
2841 PendingLoads.push_back(Chain);
2842 else
2843 DAG.setRoot(Chain);
2844 }
Bill Wendling856ff412009-12-22 00:12:37 +00002845
Benjamin Kramerf0127052010-01-05 13:12:22 +00002846 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002848 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002849 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002850 }
Bill Wendling856ff412009-12-22 00:12:37 +00002851
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002852 setValue(&I, Result);
2853 }
2854}
2855
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002856/// GetSignificand - Get the significand and build it into a floating-point
2857/// number with exponent of 1:
2858///
2859/// Op = (Op & 0x007fffff) | 0x3f800000;
2860///
2861/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002862static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002863GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002864 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2865 DAG.getConstant(0x007fffff, MVT::i32));
2866 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2867 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002868 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002869}
2870
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002871/// GetExponent - Get the exponent:
2872///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002873/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002874///
2875/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002876static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002877GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002878 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002879 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2880 DAG.getConstant(0x7f800000, MVT::i32));
2881 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002882 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002883 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2884 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002885 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002886}
2887
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002888/// getF32Constant - Get 32-bit floating point constant.
2889static SDValue
2890getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002892}
2893
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002894/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002895/// visitIntrinsicCall: I is a call instruction
2896/// Op is the associated NodeType for I
2897const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002898SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2899 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002900 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002901 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002902 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002903 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002904 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002905 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002906 getValue(I.getOperand(2)),
2907 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002908 setValue(&I, L);
2909 DAG.setRoot(L.getValue(1));
2910 return 0;
2911}
2912
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002913// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002914const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002915SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002916 SDValue Op1 = getValue(I.getOperand(1));
2917 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002918
Owen Anderson825b72b2009-08-11 20:47:22 +00002919 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002920 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002921 return 0;
2922}
Bill Wendling74c37652008-12-09 22:08:41 +00002923
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002924/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2925/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002926void
Dan Gohman46510a72010-04-15 01:51:59 +00002927SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002928 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002929 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002930
Eric Christopher551754c2010-04-16 23:37:20 +00002931 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002932 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002933 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002934
2935 // Put the exponent in the right bit position for later addition to the
2936 // final result:
2937 //
2938 // #define LOG2OFe 1.4426950f
2939 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002940 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002941 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002942 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002943
2944 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002945 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2946 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002947
2948 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002950 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002951
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002952 if (LimitFloatPrecision <= 6) {
2953 // For floating-point precision of 6:
2954 //
2955 // TwoToFractionalPartOfX =
2956 // 0.997535578f +
2957 // (0.735607626f + 0.252464424f * x) * x;
2958 //
2959 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002961 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002963 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002964 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2965 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002966 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002967 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002968
2969 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002970 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002971 TwoToFracPartOfX, IntegerPartOfX);
2972
Owen Anderson825b72b2009-08-11 20:47:22 +00002973 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002974 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2975 // For floating-point precision of 12:
2976 //
2977 // TwoToFractionalPartOfX =
2978 // 0.999892986f +
2979 // (0.696457318f +
2980 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2981 //
2982 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002984 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002985 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002986 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002987 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2988 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002989 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002990 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2991 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002992 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00002993 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002994
2995 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002996 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002997 TwoToFracPartOfX, IntegerPartOfX);
2998
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003000 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3001 // For floating-point precision of 18:
3002 //
3003 // TwoToFractionalPartOfX =
3004 // 0.999999982f +
3005 // (0.693148872f +
3006 // (0.240227044f +
3007 // (0.554906021e-1f +
3008 // (0.961591928e-2f +
3009 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3010 //
3011 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003013 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003014 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003015 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003016 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3017 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003018 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003019 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3020 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003021 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003022 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3023 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003024 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003025 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3026 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003027 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003028 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3029 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003030 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003031 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003033
3034 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003036 TwoToFracPartOfX, IntegerPartOfX);
3037
Owen Anderson825b72b2009-08-11 20:47:22 +00003038 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003039 }
3040 } else {
3041 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003042 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003043 getValue(I.getOperand(1)).getValueType(),
3044 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003045 }
3046
Dale Johannesen59e577f2008-09-05 18:38:42 +00003047 setValue(&I, result);
3048}
3049
Bill Wendling39150252008-09-09 20:39:27 +00003050/// visitLog - Lower a log intrinsic. Handles the special sequences for
3051/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003052void
Dan Gohman46510a72010-04-15 01:51:59 +00003053SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003054 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003055 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003056
Eric Christopher551754c2010-04-16 23:37:20 +00003057 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003058 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003059 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003060 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003061
3062 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003063 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003064 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003065 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003066
3067 // Get the significand and build it into a floating-point number with
3068 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003069 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003070
3071 if (LimitFloatPrecision <= 6) {
3072 // For floating-point precision of 6:
3073 //
3074 // LogofMantissa =
3075 // -1.1609546f +
3076 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003077 //
Bill Wendling39150252008-09-09 20:39:27 +00003078 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003080 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003082 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003083 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3084 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003085 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003086
Scott Michelfdc40a02009-02-17 22:15:04 +00003087 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003089 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3090 // For floating-point precision of 12:
3091 //
3092 // LogOfMantissa =
3093 // -1.7417939f +
3094 // (2.8212026f +
3095 // (-1.4699568f +
3096 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3097 //
3098 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003100 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003101 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003102 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003103 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3104 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003105 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003106 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3107 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003108 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3110 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003111 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003112
Scott Michelfdc40a02009-02-17 22:15:04 +00003113 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003114 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003115 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3116 // For floating-point precision of 18:
3117 //
3118 // LogOfMantissa =
3119 // -2.1072184f +
3120 // (4.2372794f +
3121 // (-3.7029485f +
3122 // (2.2781945f +
3123 // (-0.87823314f +
3124 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3125 //
3126 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003128 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003130 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3132 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003133 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3135 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003136 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3138 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003139 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003140 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3141 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003142 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003143 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3144 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003145 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003146
Scott Michelfdc40a02009-02-17 22:15:04 +00003147 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003149 }
3150 } else {
3151 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003152 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003153 getValue(I.getOperand(1)).getValueType(),
3154 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003155 }
3156
Dale Johannesen59e577f2008-09-05 18:38:42 +00003157 setValue(&I, result);
3158}
3159
Bill Wendling3eb59402008-09-09 00:28:24 +00003160/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3161/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003162void
Dan Gohman46510a72010-04-15 01:51:59 +00003163SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003164 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003165 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003166
Eric Christopher551754c2010-04-16 23:37:20 +00003167 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003168 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003169 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003171
Bill Wendling39150252008-09-09 20:39:27 +00003172 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003173 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003174
Bill Wendling3eb59402008-09-09 00:28:24 +00003175 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003176 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003177 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003178
Bill Wendling3eb59402008-09-09 00:28:24 +00003179 // Different possible minimax approximations of significand in
3180 // floating-point for various degrees of accuracy over [1,2].
3181 if (LimitFloatPrecision <= 6) {
3182 // For floating-point precision of 6:
3183 //
3184 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3185 //
3186 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003188 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003190 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003191 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3192 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003193 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003194
Scott Michelfdc40a02009-02-17 22:15:04 +00003195 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003197 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3198 // For floating-point precision of 12:
3199 //
3200 // Log2ofMantissa =
3201 // -2.51285454f +
3202 // (4.07009056f +
3203 // (-2.12067489f +
3204 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003205 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003206 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003208 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003209 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003210 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003211 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3212 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3215 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003216 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3218 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003219 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003220
Scott Michelfdc40a02009-02-17 22:15:04 +00003221 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003223 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3224 // For floating-point precision of 18:
3225 //
3226 // Log2ofMantissa =
3227 // -3.0400495f +
3228 // (6.1129976f +
3229 // (-5.3420409f +
3230 // (3.2865683f +
3231 // (-1.2669343f +
3232 // (0.27515199f -
3233 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3234 //
3235 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003236 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003239 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3241 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003242 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003243 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3244 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3247 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003248 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3250 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003251 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3253 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003254 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003255
Scott Michelfdc40a02009-02-17 22:15:04 +00003256 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003258 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003259 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003260 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003261 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003262 getValue(I.getOperand(1)).getValueType(),
3263 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003264 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003265
Dale Johannesen59e577f2008-09-05 18:38:42 +00003266 setValue(&I, result);
3267}
3268
Bill Wendling3eb59402008-09-09 00:28:24 +00003269/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3270/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003271void
Dan Gohman46510a72010-04-15 01:51:59 +00003272SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003273 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003274 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003275
Eric Christopher551754c2010-04-16 23:37:20 +00003276 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003277 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003278 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003280
Bill Wendling39150252008-09-09 20:39:27 +00003281 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003282 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003284 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003285
3286 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003287 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003288 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003289
3290 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003291 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003292 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003293 // Log10ofMantissa =
3294 // -0.50419619f +
3295 // (0.60948995f - 0.10380950f * x) * x;
3296 //
3297 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003299 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003301 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3303 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003304 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003305
Scott Michelfdc40a02009-02-17 22:15:04 +00003306 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003308 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3309 // For floating-point precision of 12:
3310 //
3311 // Log10ofMantissa =
3312 // -0.64831180f +
3313 // (0.91751397f +
3314 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3315 //
3316 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003320 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3322 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003323 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3325 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003326 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003327
Scott Michelfdc40a02009-02-17 22:15:04 +00003328 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003330 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003331 // For floating-point precision of 18:
3332 //
3333 // Log10ofMantissa =
3334 // -0.84299375f +
3335 // (1.5327582f +
3336 // (-1.0688956f +
3337 // (0.49102474f +
3338 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3339 //
3340 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003342 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003344 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3346 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003347 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3349 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003350 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003351 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3352 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003353 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3355 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003356 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003357
Scott Michelfdc40a02009-02-17 22:15:04 +00003358 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003360 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003361 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003362 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003363 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003364 getValue(I.getOperand(1)).getValueType(),
3365 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003366 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003367
Dale Johannesen59e577f2008-09-05 18:38:42 +00003368 setValue(&I, result);
3369}
3370
Bill Wendlinge10c8142008-09-09 22:39:21 +00003371/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3372/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003373void
Dan Gohman46510a72010-04-15 01:51:59 +00003374SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003375 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003376 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003377
Eric Christopher551754c2010-04-16 23:37:20 +00003378 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003379 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003380 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003381
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003383
3384 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3386 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003387
3388 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003390 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003391
3392 if (LimitFloatPrecision <= 6) {
3393 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003394 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003395 // TwoToFractionalPartOfX =
3396 // 0.997535578f +
3397 // (0.735607626f + 0.252464424f * x) * x;
3398 //
3399 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003403 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003404 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3405 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003406 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003408 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003410
Scott Michelfdc40a02009-02-17 22:15:04 +00003411 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003413 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3414 // For floating-point precision of 12:
3415 //
3416 // TwoToFractionalPartOfX =
3417 // 0.999892986f +
3418 // (0.696457318f +
3419 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3420 //
3421 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003423 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003425 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3427 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003428 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3430 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003431 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003433 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003435
Scott Michelfdc40a02009-02-17 22:15:04 +00003436 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003438 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3439 // For floating-point precision of 18:
3440 //
3441 // TwoToFractionalPartOfX =
3442 // 0.999999982f +
3443 // (0.693148872f +
3444 // (0.240227044f +
3445 // (0.554906021e-1f +
3446 // (0.961591928e-2f +
3447 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3448 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003450 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003451 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3454 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003455 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3457 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003458 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3460 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003461 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3463 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3466 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003467 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003469 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003471
Scott Michelfdc40a02009-02-17 22:15:04 +00003472 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003474 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003475 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003476 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003477 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003478 getValue(I.getOperand(1)).getValueType(),
3479 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003480 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003481
Dale Johannesen601d3c02008-09-05 01:48:15 +00003482 setValue(&I, result);
3483}
3484
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003485/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3486/// limited-precision mode with x == 10.0f.
3487void
Dan Gohman46510a72010-04-15 01:51:59 +00003488SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003489 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003490 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003491 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003492 bool IsExp10 = false;
3493
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003495 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003496 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3497 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3498 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3499 APFloat Ten(10.0f);
3500 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3501 }
3502 }
3503 }
3504
3505 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003506 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003507
3508 // Put the exponent in the right bit position for later addition to the
3509 // final result:
3510 //
3511 // #define LOG2OF10 3.3219281f
3512 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003514 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003516
3517 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003518 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3519 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003520
3521 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003523 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003524
3525 if (LimitFloatPrecision <= 6) {
3526 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003527 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003528 // twoToFractionalPartOfX =
3529 // 0.997535578f +
3530 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003531 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003532 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003534 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003536 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3538 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003539 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003541 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003543
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003544 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003546 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3547 // For floating-point precision of 12:
3548 //
3549 // TwoToFractionalPartOfX =
3550 // 0.999892986f +
3551 // (0.696457318f +
3552 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3553 //
3554 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003556 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003558 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3560 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003561 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3563 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003564 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003566 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003568
Scott Michelfdc40a02009-02-17 22:15:04 +00003569 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003571 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3572 // For floating-point precision of 18:
3573 //
3574 // TwoToFractionalPartOfX =
3575 // 0.999999982f +
3576 // (0.693148872f +
3577 // (0.240227044f +
3578 // (0.554906021e-1f +
3579 // (0.961591928e-2f +
3580 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3581 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003582 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003583 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003584 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003585 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003586 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3587 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003588 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3590 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003591 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3593 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003594 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3596 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003597 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3599 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003600 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003602 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003604
Scott Michelfdc40a02009-02-17 22:15:04 +00003605 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003607 }
3608 } else {
3609 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003610 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003611 getValue(I.getOperand(1)).getValueType(),
3612 getValue(I.getOperand(1)),
3613 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003614 }
3615
3616 setValue(&I, result);
3617}
3618
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003619
3620/// ExpandPowI - Expand a llvm.powi intrinsic.
3621static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3622 SelectionDAG &DAG) {
3623 // If RHS is a constant, we can expand this out to a multiplication tree,
3624 // otherwise we end up lowering to a call to __powidf2 (for example). When
3625 // optimizing for size, we only want to do this if the expansion would produce
3626 // a small number of multiplies, otherwise we do the full expansion.
3627 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3628 // Get the exponent as a positive value.
3629 unsigned Val = RHSC->getSExtValue();
3630 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003631
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003632 // powi(x, 0) -> 1.0
3633 if (Val == 0)
3634 return DAG.getConstantFP(1.0, LHS.getValueType());
3635
Dan Gohmanae541aa2010-04-15 04:33:49 +00003636 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003637 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3638 // If optimizing for size, don't insert too many multiplies. This
3639 // inserts up to 5 multiplies.
3640 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3641 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003642 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003643 // powi(x,15) generates one more multiply than it should), but this has
3644 // the benefit of being both really simple and much better than a libcall.
3645 SDValue Res; // Logically starts equal to 1.0
3646 SDValue CurSquare = LHS;
3647 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003648 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003649 if (Res.getNode())
3650 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3651 else
3652 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003653 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003654
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003655 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3656 CurSquare, CurSquare);
3657 Val >>= 1;
3658 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003659
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003660 // If the original was negative, invert the result, producing 1/(x*x*x).
3661 if (RHSC->getSExtValue() < 0)
3662 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3663 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3664 return Res;
3665 }
3666 }
3667
3668 // Otherwise, expand to a libcall.
3669 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3670}
3671
3672
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003673/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3674/// we want to emit this as a call to a named external function, return the name
3675/// otherwise lower it and return null.
3676const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003677SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003678 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003679 SDValue Res;
3680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003681 switch (Intrinsic) {
3682 default:
3683 // By default, turn this into a target intrinsic node.
3684 visitTargetIntrinsic(I, Intrinsic);
3685 return 0;
3686 case Intrinsic::vastart: visitVAStart(I); return 0;
3687 case Intrinsic::vaend: visitVAEnd(I); return 0;
3688 case Intrinsic::vacopy: visitVACopy(I); return 0;
3689 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003690 setValue(&I, DAG.getNode(ISD::RETURNADDR, 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;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003693 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003694 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003695 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003696 return 0;
3697 case Intrinsic::setjmp:
3698 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003699 case Intrinsic::longjmp:
3700 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003701 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003702 // Assert for address < 256 since we support only user defined address
3703 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003704 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003705 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003706 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003707 < 256 &&
3708 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003709 SDValue Op1 = getValue(I.getOperand(1));
3710 SDValue Op2 = getValue(I.getOperand(2));
3711 SDValue Op3 = getValue(I.getOperand(3));
3712 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3713 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003714 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003715 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003716 return 0;
3717 }
Chris Lattner824b9582008-11-21 16:42:48 +00003718 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003719 // Assert for address < 256 since we support only user defined address
3720 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003721 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003722 < 256 &&
3723 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003724 SDValue Op1 = getValue(I.getOperand(1));
3725 SDValue Op2 = getValue(I.getOperand(2));
3726 SDValue Op3 = getValue(I.getOperand(3));
3727 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3728 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003729 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003730 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003731 return 0;
3732 }
Chris Lattner824b9582008-11-21 16:42:48 +00003733 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003734 // Assert for address < 256 since we support only user defined address
3735 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003736 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003737 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003738 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003739 < 256 &&
3740 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003741 SDValue Op1 = getValue(I.getOperand(1));
3742 SDValue Op2 = getValue(I.getOperand(2));
3743 SDValue Op3 = getValue(I.getOperand(3));
3744 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3745 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003746
3747 // If the source and destination are known to not be aliases, we can
3748 // lower memmove as memcpy.
3749 uint64_t Size = -1ULL;
3750 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003751 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003752 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003753 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003754 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003755 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003756 return 0;
3757 }
3758
Mon P Wang20adc9d2010-04-04 03:10:48 +00003759 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003760 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003761 return 0;
3762 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003763 case Intrinsic::dbg_declare: {
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003764 // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None.
3765 // The real handling of this intrinsic is in FastISel.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003766 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00003767 // FIXME: Variable debug info is not supported here.
3768 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003769 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003770 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003771 return 0;
3772
Devang Patelac1ceb32009-10-09 22:42:28 +00003773 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00003774 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003775 if (!Address)
3776 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003777 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003778 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003779 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel24f20e02009-08-22 17:12:53 +00003780 // Don't handle byval struct arguments or VLAs, for example.
3781 if (!AI)
3782 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003783 DenseMap<const AllocaInst*, int>::iterator SI =
3784 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003785 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00003786 return 0; // VLAs.
3787 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003788
Chris Lattner512063d2010-04-05 06:19:28 +00003789 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3790 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3791 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003792 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003793 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003794 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003795 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003796 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3797 return 0;
3798
3799 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003800 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003801 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003802 if (!V)
3803 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003804
3805 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3806 // but do not always have a corresponding SDNode built. The SDNodeOrder
3807 // absolute, but not relative, values are different depending on whether
3808 // debug info exists.
3809 ++SDNodeOrder;
3810 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Evan Cheng31441b72010-03-29 20:48:30 +00003811 DAG.AddDbgValue(DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003812 } else {
3813 SDValue &N = NodeMap[V];
Evan Cheng31441b72010-03-29 20:48:30 +00003814 if (N.getNode())
3815 DAG.AddDbgValue(DAG.getDbgValue(Variable, N.getNode(),
3816 N.getResNo(), Offset, dl, SDNodeOrder),
3817 N.getNode());
3818 else
Devang Patel00190342010-03-15 19:15:44 +00003819 // We may expand this to cover more cases. One case where we have no
3820 // data available is an unreferenced parameter; we need this fallback.
Evan Cheng31441b72010-03-29 20:48:30 +00003821 DAG.AddDbgValue(DAG.getDbgValue(Variable,
Devang Patel00190342010-03-15 19:15:44 +00003822 UndefValue::get(V->getType()),
Evan Cheng31441b72010-03-29 20:48:30 +00003823 Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003824 }
3825
3826 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003827 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003828 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003829 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003830 // Don't handle byval struct arguments or VLAs, for example.
3831 if (!AI)
3832 return 0;
3833 DenseMap<const AllocaInst*, int>::iterator SI =
3834 FuncInfo.StaticAllocaMap.find(AI);
3835 if (SI == FuncInfo.StaticAllocaMap.end())
3836 return 0; // VLAs.
3837 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003838
Chris Lattner512063d2010-04-05 06:19:28 +00003839 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3840 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3841 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003842 return 0;
3843 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003844 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003845 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003846 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3847 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003849 SDValue Ops[1];
3850 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003851 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003852 setValue(&I, Op);
3853 DAG.setRoot(Op.getValue(1));
3854 return 0;
3855 }
3856
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003857 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003858 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003859 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003860 if (CallMBB->isLandingPad())
3861 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003862 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003863#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003864 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003865#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003866 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3867 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003868 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003869 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003870
Chris Lattner3a5815f2009-09-17 23:54:54 +00003871 // Insert the EHSELECTION instruction.
3872 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3873 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003874 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003875 Ops[1] = getRoot();
3876 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003877 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003878 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003879 return 0;
3880 }
3881
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003882 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003883 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003884 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003885 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3886 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003887 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003888 return 0;
3889 }
3890
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003891 case Intrinsic::eh_return_i32:
3892 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003893 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3894 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3895 MVT::Other,
3896 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003897 getValue(I.getOperand(1)),
3898 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003899 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003900 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003901 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003902 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003903 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003904 EVT VT = getValue(I.getOperand(1)).getValueType();
3905 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003906 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003907 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003908 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003909 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003910 TLI.getPointerTy()),
3911 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003912 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003913 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003914 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00003915 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
3916 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003917 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003918 }
Jim Grosbachca752c92010-01-28 01:45:32 +00003919 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00003920 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00003921 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00003922 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00003923 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00003924
Chris Lattner512063d2010-04-05 06:19:28 +00003925 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00003926 return 0;
3927 }
3928
Mon P Wang77cdf302008-11-10 20:54:11 +00003929 case Intrinsic::convertff:
3930 case Intrinsic::convertfsi:
3931 case Intrinsic::convertfui:
3932 case Intrinsic::convertsif:
3933 case Intrinsic::convertuif:
3934 case Intrinsic::convertss:
3935 case Intrinsic::convertsu:
3936 case Intrinsic::convertus:
3937 case Intrinsic::convertuu: {
3938 ISD::CvtCode Code = ISD::CVT_INVALID;
3939 switch (Intrinsic) {
3940 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3941 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3942 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3943 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3944 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3945 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3946 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3947 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3948 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3949 }
Owen Andersone50ed302009-08-10 22:56:29 +00003950 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00003951 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003952 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
3953 DAG.getValueType(DestVT),
3954 DAG.getValueType(getValue(Op1).getValueType()),
3955 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00003956 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003957 Code);
3958 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00003959 return 0;
3960 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003961 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00003962 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003963 getValue(I.getOperand(1)).getValueType(),
3964 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003965 return 0;
3966 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00003967 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3968 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003969 return 0;
3970 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00003971 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003972 getValue(I.getOperand(1)).getValueType(),
3973 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003974 return 0;
3975 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00003976 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003977 getValue(I.getOperand(1)).getValueType(),
3978 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003979 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003980 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003981 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003982 return 0;
3983 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003984 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003985 return 0;
3986 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003987 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003988 return 0;
3989 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003990 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003991 return 0;
3992 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003993 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003994 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003995 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003996 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003997 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00003998 case Intrinsic::convert_to_fp16:
3999 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004000 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004001 return 0;
4002 case Intrinsic::convert_from_fp16:
4003 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004004 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004005 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004006 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004007 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004008 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004009 return 0;
4010 }
4011 case Intrinsic::readcyclecounter: {
4012 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004013 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4014 DAG.getVTList(MVT::i64, MVT::Other),
4015 &Op, 1);
4016 setValue(&I, Res);
4017 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004018 return 0;
4019 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004020 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004021 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004022 getValue(I.getOperand(1)).getValueType(),
4023 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004024 return 0;
4025 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004026 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004027 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004028 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004029 return 0;
4030 }
4031 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004032 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004033 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004034 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004035 return 0;
4036 }
4037 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004038 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004039 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004040 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004041 return 0;
4042 }
4043 case Intrinsic::stacksave: {
4044 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004045 Res = DAG.getNode(ISD::STACKSAVE, dl,
4046 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4047 setValue(&I, Res);
4048 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004049 return 0;
4050 }
4051 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004052 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004053 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004054 return 0;
4055 }
Bill Wendling57344502008-11-18 11:01:33 +00004056 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004057 // Emit code into the DAG to store the stack guard onto the stack.
4058 MachineFunction &MF = DAG.getMachineFunction();
4059 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004060 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004061
Eric Christopher551754c2010-04-16 23:37:20 +00004062 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4063 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004064
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004065 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004066 MFI->setStackProtectorIndex(FI);
4067
4068 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4069
4070 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004071 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4072 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004073 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004074 setValue(&I, Res);
4075 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004076 return 0;
4077 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004078 case Intrinsic::objectsize: {
4079 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004080 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004081
4082 assert(CI && "Non-constant type in __builtin_object_size?");
4083
Eric Christopher551754c2010-04-16 23:37:20 +00004084 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004085 EVT Ty = Arg.getValueType();
4086
Eric Christopherd060b252009-12-23 02:51:48 +00004087 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004088 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004089 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004090 Res = DAG.getConstant(0, Ty);
4091
4092 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004093 return 0;
4094 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004095 case Intrinsic::var_annotation:
4096 // Discard annotate attributes
4097 return 0;
4098
4099 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004100 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004101
4102 SDValue Ops[6];
4103 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004104 Ops[1] = getValue(I.getOperand(1));
4105 Ops[2] = getValue(I.getOperand(2));
4106 Ops[3] = getValue(I.getOperand(3));
4107 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004108 Ops[5] = DAG.getSrcValue(F);
4109
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004110 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4111 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4112 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004113
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004114 setValue(&I, Res);
4115 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004116 return 0;
4117 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004118 case Intrinsic::gcroot:
4119 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004120 const Value *Alloca = I.getOperand(1);
4121 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004123 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4124 GFI->addStackRoot(FI->getIndex(), TypeMap);
4125 }
4126 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004127 case Intrinsic::gcread:
4128 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004129 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004130 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004131 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004132 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004133 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004134 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004135 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004136 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004137 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004138 return implVisitAluOverflow(I, ISD::UADDO);
4139 case Intrinsic::sadd_with_overflow:
4140 return implVisitAluOverflow(I, ISD::SADDO);
4141 case Intrinsic::usub_with_overflow:
4142 return implVisitAluOverflow(I, ISD::USUBO);
4143 case Intrinsic::ssub_with_overflow:
4144 return implVisitAluOverflow(I, ISD::SSUBO);
4145 case Intrinsic::umul_with_overflow:
4146 return implVisitAluOverflow(I, ISD::UMULO);
4147 case Intrinsic::smul_with_overflow:
4148 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004150 case Intrinsic::prefetch: {
4151 SDValue Ops[4];
4152 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004153 Ops[1] = getValue(I.getOperand(1));
4154 Ops[2] = getValue(I.getOperand(2));
4155 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004156 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004157 return 0;
4158 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004159
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004160 case Intrinsic::memory_barrier: {
4161 SDValue Ops[6];
4162 Ops[0] = getRoot();
4163 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004164 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004165
Bill Wendling4533cac2010-01-28 21:51:40 +00004166 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004167 return 0;
4168 }
4169 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004170 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004171 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004172 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004173 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004174 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004175 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004176 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004177 getValue(I.getOperand(3)),
4178 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004179 setValue(&I, L);
4180 DAG.setRoot(L.getValue(1));
4181 return 0;
4182 }
4183 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004184 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004185 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004186 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004187 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004188 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004189 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004190 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004191 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004192 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004193 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004194 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004196 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004197 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004198 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004199 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004200 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004201 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004202 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004203 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004204 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004205
4206 case Intrinsic::invariant_start:
4207 case Intrinsic::lifetime_start:
4208 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004209 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004210 return 0;
4211 case Intrinsic::invariant_end:
4212 case Intrinsic::lifetime_end:
4213 // Discard region information.
4214 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004215 }
4216}
4217
Dan Gohman46510a72010-04-15 01:51:59 +00004218void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004219 bool isTailCall,
4220 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004221 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4222 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004223 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004224 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004225 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004226
4227 TargetLowering::ArgListTy Args;
4228 TargetLowering::ArgListEntry Entry;
4229 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004230
4231 // Check whether the function can return without sret-demotion.
4232 SmallVector<EVT, 4> OutVTs;
4233 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4234 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004235 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004236 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004237
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004238 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004239 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4240
4241 SDValue DemoteStackSlot;
4242
4243 if (!CanLowerReturn) {
4244 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4245 FTy->getReturnType());
4246 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4247 FTy->getReturnType());
4248 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004249 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004250 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4251
4252 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4253 Entry.Node = DemoteStackSlot;
4254 Entry.Ty = StackSlotPtrType;
4255 Entry.isSExt = false;
4256 Entry.isZExt = false;
4257 Entry.isInReg = false;
4258 Entry.isSRet = true;
4259 Entry.isNest = false;
4260 Entry.isByVal = false;
4261 Entry.Alignment = Align;
4262 Args.push_back(Entry);
4263 RetTy = Type::getVoidTy(FTy->getContext());
4264 }
4265
Dan Gohman46510a72010-04-15 01:51:59 +00004266 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004267 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004268 SDValue ArgNode = getValue(*i);
4269 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4270
4271 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004272 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4273 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4274 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4275 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4276 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4277 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004278 Entry.Alignment = CS.getParamAlignment(attrInd);
4279 Args.push_back(Entry);
4280 }
4281
Chris Lattner512063d2010-04-05 06:19:28 +00004282 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004283 // Insert a label before the invoke call to mark the try range. This can be
4284 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004285 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004286
Jim Grosbachca752c92010-01-28 01:45:32 +00004287 // For SjLj, keep track of which landing pads go with which invokes
4288 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004289 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004290 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004291 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004292 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004293 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004294 }
4295
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 // Both PendingLoads and PendingExports must be flushed here;
4297 // this call might not return.
4298 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004299 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004300 }
4301
Dan Gohman98ca4f22009-08-05 01:29:28 +00004302 // Check if target-independent constraints permit a tail call here.
4303 // Target-dependent constraints are checked within TLI.LowerCallTo.
4304 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004305 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004306 isTailCall = false;
4307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004309 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004310 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004311 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004312 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004313 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004314 isTailCall,
4315 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004316 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004317 assert((isTailCall || Result.second.getNode()) &&
4318 "Non-null chain expected with non-tail call!");
4319 assert((Result.second.getNode() || !Result.first.getNode()) &&
4320 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004321 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004323 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004324 // The instruction result is the result of loading from the
4325 // hidden sret parameter.
4326 SmallVector<EVT, 1> PVTs;
4327 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4328
4329 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4330 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4331 EVT PtrVT = PVTs[0];
4332 unsigned NumValues = OutVTs.size();
4333 SmallVector<SDValue, 4> Values(NumValues);
4334 SmallVector<SDValue, 4> Chains(NumValues);
4335
4336 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004337 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4338 DemoteStackSlot,
4339 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004340 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004341 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004342 Values[i] = L;
4343 Chains[i] = L.getValue(1);
4344 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004345
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004346 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4347 MVT::Other, &Chains[0], NumValues);
4348 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004349
4350 // Collect the legal value parts into potentially illegal values
4351 // that correspond to the original function's return values.
4352 SmallVector<EVT, 4> RetTys;
4353 RetTy = FTy->getReturnType();
4354 ComputeValueVTs(TLI, RetTy, RetTys);
4355 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4356 SmallVector<SDValue, 4> ReturnValues;
4357 unsigned CurReg = 0;
4358 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4359 EVT VT = RetTys[I];
4360 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4361 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4362
4363 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004364 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004365 RegisterVT, VT, AssertOp);
4366 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004367 CurReg += NumRegs;
4368 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004369
Bill Wendling4533cac2010-01-28 21:51:40 +00004370 setValue(CS.getInstruction(),
4371 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4372 DAG.getVTList(&RetTys[0], RetTys.size()),
4373 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004374
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004375 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004376
4377 // As a special case, a null chain means that a tail call has been emitted and
4378 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004379 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004380 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004381 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004382 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004383
Chris Lattner512063d2010-04-05 06:19:28 +00004384 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004385 // Insert a label at the end of the invoke call to mark the try range. This
4386 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004387 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004388 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004389
4390 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004391 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 }
4393}
4394
Chris Lattner8047d9a2009-12-24 00:37:38 +00004395/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4396/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004397static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4398 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004399 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004400 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004401 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004402 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004403 if (C->isNullValue())
4404 continue;
4405 // Unknown instruction.
4406 return false;
4407 }
4408 return true;
4409}
4410
Dan Gohman46510a72010-04-15 01:51:59 +00004411static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4412 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004413 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004414
Chris Lattner8047d9a2009-12-24 00:37:38 +00004415 // Check to see if this load can be trivially constant folded, e.g. if the
4416 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004417 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004418 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004419 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004420 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004421
Dan Gohman46510a72010-04-15 01:51:59 +00004422 if (const Constant *LoadCst =
4423 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4424 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004425 return Builder.getValue(LoadCst);
4426 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004427
Chris Lattner8047d9a2009-12-24 00:37:38 +00004428 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4429 // still constant memory, the input chain can be the entry node.
4430 SDValue Root;
4431 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004432
Chris Lattner8047d9a2009-12-24 00:37:38 +00004433 // Do not serialize (non-volatile) loads of constant memory with anything.
4434 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4435 Root = Builder.DAG.getEntryNode();
4436 ConstantMemory = true;
4437 } else {
4438 // Do not serialize non-volatile loads against each other.
4439 Root = Builder.DAG.getRoot();
4440 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004441
Chris Lattner8047d9a2009-12-24 00:37:38 +00004442 SDValue Ptr = Builder.getValue(PtrVal);
4443 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4444 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004445 false /*volatile*/,
4446 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004447
Chris Lattner8047d9a2009-12-24 00:37:38 +00004448 if (!ConstantMemory)
4449 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4450 return LoadVal;
4451}
4452
4453
4454/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4455/// If so, return true and lower it, otherwise return false and it will be
4456/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004457bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004458 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4459 if (I.getNumOperands() != 4)
4460 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004461
Eric Christopher551754c2010-04-16 23:37:20 +00004462 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004463 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004464 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004465 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004466 return false;
4467
Eric Christopher551754c2010-04-16 23:37:20 +00004468 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004469
Chris Lattner8047d9a2009-12-24 00:37:38 +00004470 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4471 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004472 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4473 bool ActuallyDoIt = true;
4474 MVT LoadVT;
4475 const Type *LoadTy;
4476 switch (Size->getZExtValue()) {
4477 default:
4478 LoadVT = MVT::Other;
4479 LoadTy = 0;
4480 ActuallyDoIt = false;
4481 break;
4482 case 2:
4483 LoadVT = MVT::i16;
4484 LoadTy = Type::getInt16Ty(Size->getContext());
4485 break;
4486 case 4:
4487 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004488 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004489 break;
4490 case 8:
4491 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004492 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004493 break;
4494 /*
4495 case 16:
4496 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004497 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004498 LoadTy = VectorType::get(LoadTy, 4);
4499 break;
4500 */
4501 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004502
Chris Lattner04b091a2009-12-24 01:07:17 +00004503 // This turns into unaligned loads. We only do this if the target natively
4504 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4505 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004506
Chris Lattner04b091a2009-12-24 01:07:17 +00004507 // Require that we can find a legal MVT, and only do this if the target
4508 // supports unaligned loads of that type. Expanding into byte loads would
4509 // bloat the code.
4510 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4511 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4512 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4513 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4514 ActuallyDoIt = false;
4515 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004516
Chris Lattner04b091a2009-12-24 01:07:17 +00004517 if (ActuallyDoIt) {
4518 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4519 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004520
Chris Lattner04b091a2009-12-24 01:07:17 +00004521 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4522 ISD::SETNE);
4523 EVT CallVT = TLI.getValueType(I.getType(), true);
4524 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4525 return true;
4526 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004527 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004528
4529
Chris Lattner8047d9a2009-12-24 00:37:38 +00004530 return false;
4531}
4532
4533
Dan Gohman46510a72010-04-15 01:51:59 +00004534void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004535 const char *RenameFn = 0;
4536 if (Function *F = I.getCalledFunction()) {
4537 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004538 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004539 if (II) {
4540 if (unsigned IID = II->getIntrinsicID(F)) {
4541 RenameFn = visitIntrinsicCall(I, IID);
4542 if (!RenameFn)
4543 return;
4544 }
4545 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004546 if (unsigned IID = F->getIntrinsicID()) {
4547 RenameFn = visitIntrinsicCall(I, IID);
4548 if (!RenameFn)
4549 return;
4550 }
4551 }
4552
4553 // Check for well-known libc/libm calls. If the function is internal, it
4554 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004555 if (!F->hasLocalLinkage() && F->hasName()) {
4556 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004557 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004559 I.getOperand(1)->getType()->isFloatingPointTy() &&
4560 I.getType() == I.getOperand(1)->getType() &&
4561 I.getType() == I.getOperand(2)->getType()) {
4562 SDValue LHS = getValue(I.getOperand(1));
4563 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004564 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4565 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004566 return;
4567 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004568 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004569 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004570 I.getOperand(1)->getType()->isFloatingPointTy() &&
4571 I.getType() == I.getOperand(1)->getType()) {
4572 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004573 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4574 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004575 return;
4576 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004577 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004578 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004579 I.getOperand(1)->getType()->isFloatingPointTy() &&
4580 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004581 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004582 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004583 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4584 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004585 return;
4586 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004587 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004589 I.getOperand(1)->getType()->isFloatingPointTy() &&
4590 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004591 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004592 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004593 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4594 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 return;
4596 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004597 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4598 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004599 I.getOperand(1)->getType()->isFloatingPointTy() &&
4600 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004601 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004602 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004603 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4604 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004605 return;
4606 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004607 } else if (Name == "memcmp") {
4608 if (visitMemCmpCall(I))
4609 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004610 }
4611 }
Eric Christopher551754c2010-04-16 23:37:20 +00004612 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004613 visitInlineAsm(&I);
4614 return;
4615 }
4616
4617 SDValue Callee;
4618 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004619 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004620 else
Bill Wendling056292f2008-09-16 21:48:12 +00004621 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004622
Bill Wendling0d580132009-12-23 01:28:19 +00004623 // Check if we can potentially perform a tail call. More detailed checking is
4624 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004625 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004626}
4627
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004628/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004629/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004630/// Chain/Flag as the input and updates them for the output Chain/Flag.
4631/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004632SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004633 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634 // Assemble the legal parts into the final values.
4635 SmallVector<SDValue, 4> Values(ValueVTs.size());
4636 SmallVector<SDValue, 8> Parts;
4637 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4638 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004639 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004640 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004641 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004642
4643 Parts.resize(NumRegs);
4644 for (unsigned i = 0; i != NumRegs; ++i) {
4645 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004646 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004647 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004648 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004649 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 *Flag = P.getValue(2);
4651 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004653 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004655 // If the source register was virtual and if we know something about it,
4656 // add an assert node.
4657 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4658 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4659 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4660 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4661 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4662 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 unsigned RegSize = RegisterVT.getSizeInBits();
4665 unsigned NumSignBits = LOI.NumSignBits;
4666 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 // FIXME: We capture more information than the dag can represent. For
4669 // now, just use the tightest assertzext/assertsext possible.
4670 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004673 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004674 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004675 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004677 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004678 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004679 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004680 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004681 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004682 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004683 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004685 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004686 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004687 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004688
Bill Wendling4533cac2010-01-28 21:51:40 +00004689 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004690 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004692 }
4693 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004694
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004695 Parts[i] = P;
4696 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004697
Bill Wendling46ada192010-03-02 01:55:18 +00004698 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004699 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004700 Part += NumRegs;
4701 Parts.clear();
4702 }
4703
Bill Wendling4533cac2010-01-28 21:51:40 +00004704 return DAG.getNode(ISD::MERGE_VALUES, dl,
4705 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4706 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707}
4708
4709/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004710/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004711/// Chain/Flag as the input and updates them for the output Chain/Flag.
4712/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004713void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004714 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715 // Get the list of the values's legal parts.
4716 unsigned NumRegs = Regs.size();
4717 SmallVector<SDValue, 8> Parts(NumRegs);
4718 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004719 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004720 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004721 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004722
Bill Wendling46ada192010-03-02 01:55:18 +00004723 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004724 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004725 &Parts[Part], NumParts, RegisterVT);
4726 Part += NumParts;
4727 }
4728
4729 // Copy the parts into the registers.
4730 SmallVector<SDValue, 8> Chains(NumRegs);
4731 for (unsigned i = 0; i != NumRegs; ++i) {
4732 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004733 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004734 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004735 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004736 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004737 *Flag = Part.getValue(1);
4738 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004739
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004740 Chains[i] = Part.getValue(0);
4741 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004742
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004743 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004744 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004745 // flagged to it. That is the CopyToReg nodes and the user are considered
4746 // a single scheduling unit. If we create a TokenFactor and return it as
4747 // chain, then the TokenFactor is both a predecessor (operand) of the
4748 // user as well as a successor (the TF operands are flagged to the user).
4749 // c1, f1 = CopyToReg
4750 // c2, f2 = CopyToReg
4751 // c3 = TokenFactor c1, c2
4752 // ...
4753 // = op c3, ..., f2
4754 Chain = Chains[NumRegs-1];
4755 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004756 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757}
4758
4759/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004760/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004762void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4763 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004764 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004765 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004766 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004767 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004768 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004769 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004770 Ops.push_back(Res);
4771
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004772 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004773 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004774 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004775 for (unsigned i = 0; i != NumRegs; ++i) {
4776 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004777 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004778 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779 }
4780}
4781
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004782/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004783/// i.e. it isn't a stack pointer or some other special register, return the
4784/// register class for the register. Otherwise, return null.
4785static const TargetRegisterClass *
4786isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4787 const TargetLowering &TLI,
4788 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790 const TargetRegisterClass *FoundRC = 0;
4791 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4792 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004793 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004794
4795 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004796 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4798 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4799 I != E; ++I) {
4800 if (TLI.isTypeLegal(*I)) {
4801 // If we have already found this register in a different register class,
4802 // choose the one with the largest VT specified. For example, on
4803 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 ThisVT = *I;
4806 break;
4807 }
4808 }
4809 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004810
Owen Anderson825b72b2009-08-11 20:47:22 +00004811 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004812
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813 // NOTE: This isn't ideal. In particular, this might allocate the
4814 // frame pointer in functions that need it (due to them not being taken
4815 // out of allocation, because a variable sized allocation hasn't been seen
4816 // yet). This is a slight code pessimization, but should still work.
4817 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4818 E = RC->allocation_order_end(MF); I != E; ++I)
4819 if (*I == Reg) {
4820 // We found a matching register class. Keep looking at others in case
4821 // we find one with larger registers that this physreg is also in.
4822 FoundRC = RC;
4823 FoundVT = ThisVT;
4824 break;
4825 }
4826 }
4827 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004828}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004829
4830
4831namespace llvm {
4832/// AsmOperandInfo - This contains information for each constraint that we are
4833/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004834class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004835 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004836public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837 /// CallOperand - If this is the result output operand or a clobber
4838 /// this is null, otherwise it is the incoming operand to the CallInst.
4839 /// This gets modified as the asm is processed.
4840 SDValue CallOperand;
4841
4842 /// AssignedRegs - If this is a register or register class operand, this
4843 /// contains the set of register corresponding to the operand.
4844 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004846 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4847 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4848 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004849
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4851 /// busy in OutputRegs/InputRegs.
4852 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004853 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 std::set<unsigned> &InputRegs,
4855 const TargetRegisterInfo &TRI) const {
4856 if (isOutReg) {
4857 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4858 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4859 }
4860 if (isInReg) {
4861 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4862 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4863 }
4864 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004865
Owen Andersone50ed302009-08-10 22:56:29 +00004866 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004867 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004869 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004870 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004871 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004873
Chris Lattner81249c92008-10-17 17:05:25 +00004874 if (isa<BasicBlock>(CallOperandVal))
4875 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004876
Chris Lattner81249c92008-10-17 17:05:25 +00004877 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004878
Chris Lattner81249c92008-10-17 17:05:25 +00004879 // If this is an indirect operand, the operand is a pointer to the
4880 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004881 if (isIndirect) {
4882 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4883 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004884 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004885 OpTy = PtrTy->getElementType();
4886 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004887
Chris Lattner81249c92008-10-17 17:05:25 +00004888 // If OpTy is not a single value, it may be a struct/union that we
4889 // can tile with integers.
4890 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4891 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4892 switch (BitSize) {
4893 default: break;
4894 case 1:
4895 case 8:
4896 case 16:
4897 case 32:
4898 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004899 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004900 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004901 break;
4902 }
4903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004904
Chris Lattner81249c92008-10-17 17:05:25 +00004905 return TLI.getValueType(OpTy, true);
4906 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004908private:
4909 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4910 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004911 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004912 const TargetRegisterInfo &TRI) {
4913 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4914 Regs.insert(Reg);
4915 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4916 for (; *Aliases; ++Aliases)
4917 Regs.insert(*Aliases);
4918 }
4919};
4920} // end llvm namespace.
4921
4922
4923/// GetRegistersForValue - Assign registers (virtual or physical) for the
4924/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004925/// register allocator to handle the assignment process. However, if the asm
4926/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004927/// allocation. This produces generally horrible, but correct, code.
4928///
4929/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004930/// Input and OutputRegs are the set of already allocated physical registers.
4931///
Dan Gohman2048b852009-11-23 18:04:58 +00004932void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004933GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004934 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004936 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004938 // Compute whether this value requires an input register, an output register,
4939 // or both.
4940 bool isOutReg = false;
4941 bool isInReg = false;
4942 switch (OpInfo.Type) {
4943 case InlineAsm::isOutput:
4944 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004945
4946 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004947 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004948 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004949 break;
4950 case InlineAsm::isInput:
4951 isInReg = true;
4952 isOutReg = false;
4953 break;
4954 case InlineAsm::isClobber:
4955 isOutReg = true;
4956 isInReg = true;
4957 break;
4958 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004959
4960
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004961 MachineFunction &MF = DAG.getMachineFunction();
4962 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004963
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004964 // If this is a constraint for a single physreg, or a constraint for a
4965 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004966 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004967 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4968 OpInfo.ConstraintVT);
4969
4970 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004971 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004972 // If this is a FP input in an integer register (or visa versa) insert a bit
4973 // cast of the input value. More generally, handle any case where the input
4974 // value disagrees with the register class we plan to stick this in.
4975 if (OpInfo.Type == InlineAsm::isInput &&
4976 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004977 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004978 // types are identical size, use a bitcast to convert (e.g. two differing
4979 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004980 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004981 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004982 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004983 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004984 OpInfo.ConstraintVT = RegVT;
4985 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4986 // If the input is a FP value and we want it in FP registers, do a
4987 // bitcast to the corresponding integer type. This turns an f64 value
4988 // into i64, which can be passed with two i32 values on a 32-bit
4989 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004990 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00004991 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004992 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004993 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004994 OpInfo.ConstraintVT = RegVT;
4995 }
4996 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004997
Owen Anderson23b9b192009-08-12 00:36:31 +00004998 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004999 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005000
Owen Andersone50ed302009-08-10 22:56:29 +00005001 EVT RegVT;
5002 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005003
5004 // If this is a constraint for a specific physical register, like {r17},
5005 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005006 if (unsigned AssignedReg = PhysReg.first) {
5007 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005008 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005009 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005010
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005011 // Get the actual register value type. This is important, because the user
5012 // may have asked for (e.g.) the AX register in i32 type. We need to
5013 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005014 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005016 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005017 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005018
5019 // If this is an expanded reference, add the rest of the regs to Regs.
5020 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005021 TargetRegisterClass::iterator I = RC->begin();
5022 for (; *I != AssignedReg; ++I)
5023 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005024
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005025 // Already added the first reg.
5026 --NumRegs; ++I;
5027 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005028 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005029 Regs.push_back(*I);
5030 }
5031 }
Bill Wendling651ad132009-12-22 01:25:10 +00005032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005033 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5034 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5035 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5036 return;
5037 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005038
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005039 // Otherwise, if this was a reference to an LLVM register class, create vregs
5040 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005041 if (const TargetRegisterClass *RC = PhysReg.second) {
5042 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005043 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005044 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045
Evan Chengfb112882009-03-23 08:01:15 +00005046 // Create the appropriate number of virtual registers.
5047 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5048 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005049 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005050
Evan Chengfb112882009-03-23 08:01:15 +00005051 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5052 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005053 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005054
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005055 // This is a reference to a register class that doesn't directly correspond
5056 // to an LLVM register class. Allocate NumRegs consecutive, available,
5057 // registers from the class.
5058 std::vector<unsigned> RegClassRegs
5059 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5060 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005062 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5063 unsigned NumAllocated = 0;
5064 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5065 unsigned Reg = RegClassRegs[i];
5066 // See if this register is available.
5067 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5068 (isInReg && InputRegs.count(Reg))) { // Already used.
5069 // Make sure we find consecutive registers.
5070 NumAllocated = 0;
5071 continue;
5072 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005074 // Check to see if this register is allocatable (i.e. don't give out the
5075 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005076 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5077 if (!RC) { // Couldn't allocate this register.
5078 // Reset NumAllocated to make sure we return consecutive registers.
5079 NumAllocated = 0;
5080 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005083 // Okay, this register is good, we can use it.
5084 ++NumAllocated;
5085
5086 // If we allocated enough consecutive registers, succeed.
5087 if (NumAllocated == NumRegs) {
5088 unsigned RegStart = (i-NumAllocated)+1;
5089 unsigned RegEnd = i+1;
5090 // Mark all of the allocated registers used.
5091 for (unsigned i = RegStart; i != RegEnd; ++i)
5092 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005093
5094 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005095 OpInfo.ConstraintVT);
5096 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5097 return;
5098 }
5099 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005100
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005101 // Otherwise, we couldn't allocate enough registers for this.
5102}
5103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005104/// visitInlineAsm - Handle a call to an InlineAsm object.
5105///
Dan Gohman46510a72010-04-15 01:51:59 +00005106void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5107 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005108
5109 /// ConstraintOperands - Information about all of the constraints.
5110 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005112 std::set<unsigned> OutputRegs, InputRegs;
5113
5114 // Do a prepass over the constraints, canonicalizing them, and building up the
5115 // ConstraintOperands list.
5116 std::vector<InlineAsm::ConstraintInfo>
5117 ConstraintInfos = IA->ParseConstraints();
5118
Evan Chengda43bcf2008-09-24 00:05:32 +00005119 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005120
Chris Lattner6c147292009-04-30 00:48:50 +00005121 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005122
Chris Lattner6c147292009-04-30 00:48:50 +00005123 // We won't need to flush pending loads if this asm doesn't touch
5124 // memory and is nonvolatile.
5125 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005126 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005127 else
5128 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005130 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5131 unsigned ResNo = 0; // ResNo - The result number of the next output.
5132 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5133 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5134 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005135
Owen Anderson825b72b2009-08-11 20:47:22 +00005136 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005137
5138 // Compute the value type for each operand.
5139 switch (OpInfo.Type) {
5140 case InlineAsm::isOutput:
5141 // Indirect outputs just consume an argument.
5142 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005143 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005144 break;
5145 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 // The return value of the call is this value. As such, there is no
5148 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005149 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005150 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005151 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5152 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5153 } else {
5154 assert(ResNo == 0 && "Asm only has one result!");
5155 OpVT = TLI.getValueType(CS.getType());
5156 }
5157 ++ResNo;
5158 break;
5159 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005160 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005161 break;
5162 case InlineAsm::isClobber:
5163 // Nothing to do.
5164 break;
5165 }
5166
5167 // If this is an input or an indirect output, process the call argument.
5168 // BasicBlocks are labels, currently appearing only in asm's.
5169 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005170 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005171 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5172
Dan Gohman46510a72010-04-15 01:51:59 +00005173 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005175 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005176 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005177 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005178
Owen Anderson1d0be152009-08-13 21:58:54 +00005179 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005182 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005183 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005184
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005185 // Second pass over the constraints: compute which constraint option to use
5186 // and assign registers to constraints that want a specific physreg.
5187 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5188 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005189
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005190 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005191 // matching input. If their types mismatch, e.g. one is an integer, the
5192 // other is floating point, or their sizes are different, flag it as an
5193 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005194 if (OpInfo.hasMatchingInput()) {
5195 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005196
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005197 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005198 if ((OpInfo.ConstraintVT.isInteger() !=
5199 Input.ConstraintVT.isInteger()) ||
5200 (OpInfo.ConstraintVT.getSizeInBits() !=
5201 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005202 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005203 " with a matching output constraint of"
5204 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005205 }
5206 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005207 }
5208 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005210 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005211 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005212
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005213 // If this is a memory input, and if the operand is not indirect, do what we
5214 // need to to provide an address for the memory input.
5215 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5216 !OpInfo.isIndirect) {
5217 assert(OpInfo.Type == InlineAsm::isInput &&
5218 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // Memory operands really want the address of the value. If we don't have
5221 // an indirect input, put it in the constpool if we can, otherwise spill
5222 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005224 // If the operand is a float, integer, or vector constant, spill to a
5225 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005226 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5228 isa<ConstantVector>(OpVal)) {
5229 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5230 TLI.getPointerTy());
5231 } else {
5232 // Otherwise, create a stack slot and emit a store to it before the
5233 // asm.
5234 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005235 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005236 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5237 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005238 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005239 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005240 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005241 OpInfo.CallOperand, StackSlot, NULL, 0,
5242 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 OpInfo.CallOperand = StackSlot;
5244 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005245
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005246 // There is no longer a Value* corresponding to this operand.
5247 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005248
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005249 // It is now an indirect operand.
5250 OpInfo.isIndirect = true;
5251 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005253 // If this constraint is for a specific register, allocate it before
5254 // anything else.
5255 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005256 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005258
Bill Wendling651ad132009-12-22 01:25:10 +00005259 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005262 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005263 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5264 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 // C_Register operands have already been allocated, Other/Memory don't need
5267 // to be.
5268 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005269 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005270 }
5271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005272 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5273 std::vector<SDValue> AsmNodeOperands;
5274 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5275 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005276 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5277 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005278
Chris Lattnerdecc2672010-04-07 05:20:54 +00005279 // If we have a !srcloc metadata node associated with it, we want to attach
5280 // this to the ultimately generated inline asm machineinstr. To do this, we
5281 // pass in the third operand as this (potentially null) inline asm MDNode.
5282 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5283 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005284
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005285 // Loop over all of the inputs, copying the operand values into the
5286 // appropriate registers and processing the output regs.
5287 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005288
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005289 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5290 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005292 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5293 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5294
5295 switch (OpInfo.Type) {
5296 case InlineAsm::isOutput: {
5297 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5298 OpInfo.ConstraintType != TargetLowering::C_Register) {
5299 // Memory output, or 'other' output (e.g. 'X' constraint).
5300 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5301
5302 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005303 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5304 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 TLI.getPointerTy()));
5306 AsmNodeOperands.push_back(OpInfo.CallOperand);
5307 break;
5308 }
5309
5310 // Otherwise, this is a register or register class output.
5311
5312 // Copy the output from the appropriate register. Find a register that
5313 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005314 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005315 report_fatal_error("Couldn't allocate output reg for constraint '" +
5316 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005317
5318 // If this is an indirect operand, store through the pointer after the
5319 // asm.
5320 if (OpInfo.isIndirect) {
5321 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5322 OpInfo.CallOperandVal));
5323 } else {
5324 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005325 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005326 // Concatenate this output onto the outputs list.
5327 RetValRegs.append(OpInfo.AssignedRegs);
5328 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005330 // Add information to the INLINEASM node to know that this register is
5331 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005332 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005333 InlineAsm::Kind_RegDefEarlyClobber :
5334 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005335 false,
5336 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005337 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005338 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005339 break;
5340 }
5341 case InlineAsm::isInput: {
5342 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005343
Chris Lattner6bdcda32008-10-17 16:47:46 +00005344 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 // If this is required to match an output register we have already set,
5346 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005347 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005348
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005349 // Scan until we find the definition we already emitted of this operand.
5350 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005351 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 for (; OperandNo; --OperandNo) {
5353 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005354 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005355 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005356 assert((InlineAsm::isRegDefKind(OpFlag) ||
5357 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5358 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005359 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005360 }
5361
Evan Cheng697cbbf2009-03-20 18:03:34 +00005362 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005363 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005364 if (InlineAsm::isRegDefKind(OpFlag) ||
5365 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005366 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005367 if (OpInfo.isIndirect) {
5368 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005369 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005370 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5371 " don't know how to handle tied "
5372 "indirect register inputs");
5373 }
5374
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375 RegsForValue MatchedRegs;
5376 MatchedRegs.TLI = &TLI;
5377 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005378 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005379 MatchedRegs.RegVTs.push_back(RegVT);
5380 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005381 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005382 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005383 MatchedRegs.Regs.push_back
5384 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005385
5386 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005387 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005388 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005389 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005390 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005391 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005393 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005394
5395 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5396 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5397 "Unexpected number of operands");
5398 // Add information to the INLINEASM node to know about this input.
5399 // See InlineAsm.h isUseOperandTiedToDef.
5400 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5401 OpInfo.getMatchedOperand());
5402 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5403 TLI.getPointerTy()));
5404 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5405 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005406 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005407
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005408 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005409 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005411
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005412 std::vector<SDValue> Ops;
5413 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005414 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005415 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005416 report_fatal_error("Invalid operand for inline asm constraint '" +
5417 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005418
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005419 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005420 unsigned ResOpType =
5421 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005422 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005423 TLI.getPointerTy()));
5424 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5425 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005426 }
5427
5428 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005429 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5430 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5431 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005432
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005433 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005434 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005435 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005436 TLI.getPointerTy()));
5437 AsmNodeOperands.push_back(InOperandVal);
5438 break;
5439 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005441 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5442 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5443 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005444 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 "Don't know how to handle indirect register inputs yet!");
5446
5447 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005448 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005449 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005450 report_fatal_error("Couldn't allocate input reg for constraint '" +
5451 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452
Dale Johannesen66978ee2009-01-31 02:22:37 +00005453 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005454 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005455
Chris Lattnerdecc2672010-04-07 05:20:54 +00005456 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005457 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005458 break;
5459 }
5460 case InlineAsm::isClobber: {
5461 // Add the clobbered value to the operand list, so that the register
5462 // allocator is aware that the physreg got clobbered.
5463 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005464 OpInfo.AssignedRegs.AddInlineAsmOperands(
5465 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005466 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005467 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 break;
5469 }
5470 }
5471 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005472
Chris Lattnerdecc2672010-04-07 05:20:54 +00005473 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 AsmNodeOperands[0] = Chain;
5475 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005476
Dale Johannesen66978ee2009-01-31 02:22:37 +00005477 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005478 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005479 &AsmNodeOperands[0], AsmNodeOperands.size());
5480 Flag = Chain.getValue(1);
5481
5482 // If this asm returns a register value, copy the result from that register
5483 // and set it as the value of the call.
5484 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005485 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005486 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005487
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005488 // FIXME: Why don't we do this for inline asms with MRVs?
5489 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005490 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005491
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005492 // If any of the results of the inline asm is a vector, it may have the
5493 // wrong width/num elts. This can happen for register classes that can
5494 // contain multiple different value types. The preg or vreg allocated may
5495 // not have the same VT as was expected. Convert it to the right type
5496 // with bit_convert.
5497 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005498 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005499 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005500
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005501 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005502 ResultType.isInteger() && Val.getValueType().isInteger()) {
5503 // If a result value was tied to an input value, the computed result may
5504 // have a wider width than the expected result. Extract the relevant
5505 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005506 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005507 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005508
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005509 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005510 }
Dan Gohman95915732008-10-18 01:03:45 +00005511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005512 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005513 // Don't need to use this as a chain in this case.
5514 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5515 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005517
Dan Gohman46510a72010-04-15 01:51:59 +00005518 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005519
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005520 // Process indirect outputs, first output all of the flagged copies out of
5521 // physregs.
5522 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5523 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005524 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005525 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005526 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005527 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5528 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005530 // Emit the non-flagged stores from the physregs.
5531 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005532 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5533 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5534 StoresToEmit[i].first,
5535 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005536 StoresToEmit[i].second, 0,
5537 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005538 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005539 }
5540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005543 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005544
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005545 DAG.setRoot(Chain);
5546}
5547
Dan Gohman46510a72010-04-15 01:51:59 +00005548void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005549 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5550 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005551 getValue(I.getOperand(1)),
5552 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553}
5554
Dan Gohman46510a72010-04-15 01:51:59 +00005555void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005556 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5557 getRoot(), getValue(I.getOperand(0)),
5558 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005559 setValue(&I, V);
5560 DAG.setRoot(V.getValue(1));
5561}
5562
Dan Gohman46510a72010-04-15 01:51:59 +00005563void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005564 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5565 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005566 getValue(I.getOperand(1)),
5567 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568}
5569
Dan Gohman46510a72010-04-15 01:51:59 +00005570void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005571 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5572 MVT::Other, getRoot(),
5573 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005574 getValue(I.getOperand(2)),
5575 DAG.getSrcValue(I.getOperand(1)),
5576 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005577}
5578
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005579/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005580/// implementation, which just calls LowerCall.
5581/// FIXME: When all targets are
5582/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005583std::pair<SDValue, SDValue>
5584TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5585 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005586 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005587 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005588 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005589 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005590 ArgListTy &Args, SelectionDAG &DAG,
5591 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005592 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005593 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005595 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005596 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5597 for (unsigned Value = 0, NumValues = ValueVTs.size();
5598 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005599 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005600 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005601 SDValue Op = SDValue(Args[i].Node.getNode(),
5602 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 ISD::ArgFlagsTy Flags;
5604 unsigned OriginalAlignment =
5605 getTargetData()->getABITypeAlignment(ArgTy);
5606
5607 if (Args[i].isZExt)
5608 Flags.setZExt();
5609 if (Args[i].isSExt)
5610 Flags.setSExt();
5611 if (Args[i].isInReg)
5612 Flags.setInReg();
5613 if (Args[i].isSRet)
5614 Flags.setSRet();
5615 if (Args[i].isByVal) {
5616 Flags.setByVal();
5617 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5618 const Type *ElementTy = Ty->getElementType();
5619 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005620 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005621 // For ByVal, alignment should come from FE. BE will guess if this
5622 // info is not there but there are cases it cannot get right.
5623 if (Args[i].Alignment)
5624 FrameAlign = Args[i].Alignment;
5625 Flags.setByValAlign(FrameAlign);
5626 Flags.setByValSize(FrameSize);
5627 }
5628 if (Args[i].isNest)
5629 Flags.setNest();
5630 Flags.setOrigAlign(OriginalAlignment);
5631
Owen Anderson23b9b192009-08-12 00:36:31 +00005632 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5633 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005634 SmallVector<SDValue, 4> Parts(NumParts);
5635 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5636
5637 if (Args[i].isSExt)
5638 ExtendKind = ISD::SIGN_EXTEND;
5639 else if (Args[i].isZExt)
5640 ExtendKind = ISD::ZERO_EXTEND;
5641
Bill Wendling46ada192010-03-02 01:55:18 +00005642 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005643 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005644
Dan Gohman98ca4f22009-08-05 01:29:28 +00005645 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005646 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005647 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5648 if (NumParts > 1 && j == 0)
5649 MyFlags.Flags.setSplit();
5650 else if (j != 0)
5651 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652
Dan Gohman98ca4f22009-08-05 01:29:28 +00005653 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 }
5655 }
5656 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005657
Dan Gohman98ca4f22009-08-05 01:29:28 +00005658 // Handle the incoming return values from the call.
5659 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005660 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005662 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005663 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005664 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5665 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005666 for (unsigned i = 0; i != NumRegs; ++i) {
5667 ISD::InputArg MyFlags;
5668 MyFlags.VT = RegisterVT;
5669 MyFlags.Used = isReturnValueUsed;
5670 if (RetSExt)
5671 MyFlags.Flags.setSExt();
5672 if (RetZExt)
5673 MyFlags.Flags.setZExt();
5674 if (isInreg)
5675 MyFlags.Flags.setInReg();
5676 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005678 }
5679
Dan Gohman98ca4f22009-08-05 01:29:28 +00005680 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005681 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005682 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005683
5684 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005685 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005686 "LowerCall didn't return a valid chain!");
5687 assert((!isTailCall || InVals.empty()) &&
5688 "LowerCall emitted a return value for a tail call!");
5689 assert((isTailCall || InVals.size() == Ins.size()) &&
5690 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005691
5692 // For a tail call, the return value is merely live-out and there aren't
5693 // any nodes in the DAG representing it. Return a special value to
5694 // indicate that a tail call has been emitted and no more Instructions
5695 // should be processed in the current block.
5696 if (isTailCall) {
5697 DAG.setRoot(Chain);
5698 return std::make_pair(SDValue(), SDValue());
5699 }
5700
Evan Chengaf1871f2010-03-11 19:38:18 +00005701 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5702 assert(InVals[i].getNode() &&
5703 "LowerCall emitted a null value!");
5704 assert(Ins[i].VT == InVals[i].getValueType() &&
5705 "LowerCall emitted a value with the wrong type!");
5706 });
5707
Dan Gohman98ca4f22009-08-05 01:29:28 +00005708 // Collect the legal value parts into potentially illegal values
5709 // that correspond to the original function's return values.
5710 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5711 if (RetSExt)
5712 AssertOp = ISD::AssertSext;
5713 else if (RetZExt)
5714 AssertOp = ISD::AssertZext;
5715 SmallVector<SDValue, 4> ReturnValues;
5716 unsigned CurReg = 0;
5717 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005718 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005719 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5720 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005721
Bill Wendling46ada192010-03-02 01:55:18 +00005722 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005723 NumRegs, RegisterVT, VT,
5724 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005725 CurReg += NumRegs;
5726 }
5727
5728 // For a function returning void, there is no return value. We can't create
5729 // such a node, so we just return a null return value in that case. In
5730 // that case, nothing will actualy look at the value.
5731 if (ReturnValues.empty())
5732 return std::make_pair(SDValue(), Chain);
5733
5734 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5735 DAG.getVTList(&RetTys[0], RetTys.size()),
5736 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005737 return std::make_pair(Res, Chain);
5738}
5739
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005740void TargetLowering::LowerOperationWrapper(SDNode *N,
5741 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005742 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005743 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005744 if (Res.getNode())
5745 Results.push_back(Res);
5746}
5747
Dan Gohmand858e902010-04-17 15:26:15 +00005748SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005749 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005750 return SDValue();
5751}
5752
Dan Gohman46510a72010-04-15 01:51:59 +00005753void
5754SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005755 SDValue Op = getValue(V);
5756 assert((Op.getOpcode() != ISD::CopyFromReg ||
5757 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5758 "Copy from a reg to the same reg!");
5759 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5760
Owen Anderson23b9b192009-08-12 00:36:31 +00005761 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005762 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005763 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005764 PendingExports.push_back(Chain);
5765}
5766
5767#include "llvm/CodeGen/SelectionDAGISel.h"
5768
Dan Gohman46510a72010-04-15 01:51:59 +00005769void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005771 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005772 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005773 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005774 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005775 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005776 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005777
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005778 // Check whether the function can return without sret-demotion.
5779 SmallVector<EVT, 4> OutVTs;
5780 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005781 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005782 OutVTs, OutsFlags, TLI);
5783 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5784
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005785 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005786 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005787 if (!FLI.CanLowerReturn) {
5788 // Put in an sret pointer parameter before all the other parameters.
5789 SmallVector<EVT, 1> ValueVTs;
5790 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5791
5792 // NOTE: Assuming that a pointer will never break down to more than one VT
5793 // or one register.
5794 ISD::ArgFlagsTy Flags;
5795 Flags.setSRet();
5796 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5797 ISD::InputArg RetArg(Flags, RegisterVT, true);
5798 Ins.push_back(RetArg);
5799 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005800
Dan Gohman98ca4f22009-08-05 01:29:28 +00005801 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005802 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005803 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005804 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005805 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005806 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5807 bool isArgValueUsed = !I->use_empty();
5808 for (unsigned Value = 0, NumValues = ValueVTs.size();
5809 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005810 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005811 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005812 ISD::ArgFlagsTy Flags;
5813 unsigned OriginalAlignment =
5814 TD->getABITypeAlignment(ArgTy);
5815
5816 if (F.paramHasAttr(Idx, Attribute::ZExt))
5817 Flags.setZExt();
5818 if (F.paramHasAttr(Idx, Attribute::SExt))
5819 Flags.setSExt();
5820 if (F.paramHasAttr(Idx, Attribute::InReg))
5821 Flags.setInReg();
5822 if (F.paramHasAttr(Idx, Attribute::StructRet))
5823 Flags.setSRet();
5824 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5825 Flags.setByVal();
5826 const PointerType *Ty = cast<PointerType>(I->getType());
5827 const Type *ElementTy = Ty->getElementType();
5828 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5829 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5830 // For ByVal, alignment should be passed from FE. BE will guess if
5831 // this info is not there but there are cases it cannot get right.
5832 if (F.getParamAlignment(Idx))
5833 FrameAlign = F.getParamAlignment(Idx);
5834 Flags.setByValAlign(FrameAlign);
5835 Flags.setByValSize(FrameSize);
5836 }
5837 if (F.paramHasAttr(Idx, Attribute::Nest))
5838 Flags.setNest();
5839 Flags.setOrigAlign(OriginalAlignment);
5840
Owen Anderson23b9b192009-08-12 00:36:31 +00005841 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5842 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005843 for (unsigned i = 0; i != NumRegs; ++i) {
5844 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5845 if (NumRegs > 1 && i == 0)
5846 MyFlags.Flags.setSplit();
5847 // if it isn't first piece, alignment must be 1
5848 else if (i > 0)
5849 MyFlags.Flags.setOrigAlign(1);
5850 Ins.push_back(MyFlags);
5851 }
5852 }
5853 }
5854
5855 // Call the target to set up the argument values.
5856 SmallVector<SDValue, 8> InVals;
5857 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5858 F.isVarArg(), Ins,
5859 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005860
5861 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005862 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005863 "LowerFormalArguments didn't return a valid chain!");
5864 assert(InVals.size() == Ins.size() &&
5865 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005866 DEBUG({
5867 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5868 assert(InVals[i].getNode() &&
5869 "LowerFormalArguments emitted a null value!");
5870 assert(Ins[i].VT == InVals[i].getValueType() &&
5871 "LowerFormalArguments emitted a value with the wrong type!");
5872 }
5873 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005874
Dan Gohman5e866062009-08-06 15:37:27 +00005875 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005876 DAG.setRoot(NewRoot);
5877
5878 // Set up the argument values.
5879 unsigned i = 0;
5880 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005881 if (!FLI.CanLowerReturn) {
5882 // Create a virtual register for the sret pointer, and put in a copy
5883 // from the sret argument into it.
5884 SmallVector<EVT, 1> ValueVTs;
5885 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5886 EVT VT = ValueVTs[0];
5887 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5888 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005889 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005890 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005891
Dan Gohman2048b852009-11-23 18:04:58 +00005892 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005893 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5894 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5895 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005896 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5897 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005898 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005899
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005900 // i indexes lowered arguments. Bump it past the hidden sret argument.
5901 // Idx indexes LLVM arguments. Don't touch it.
5902 ++i;
5903 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005904
Dan Gohman46510a72010-04-15 01:51:59 +00005905 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005906 ++I, ++Idx) {
5907 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005908 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005909 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005910 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005911 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005912 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005913 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5914 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005915
5916 if (!I->use_empty()) {
5917 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5918 if (F.paramHasAttr(Idx, Attribute::SExt))
5919 AssertOp = ISD::AssertSext;
5920 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5921 AssertOp = ISD::AssertZext;
5922
Bill Wendling46ada192010-03-02 01:55:18 +00005923 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00005924 NumParts, PartVT, VT,
5925 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005926 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005927
Dan Gohman98ca4f22009-08-05 01:29:28 +00005928 i += NumParts;
5929 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005930
Dan Gohman98ca4f22009-08-05 01:29:28 +00005931 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00005932 SDValue Res;
5933 if (!ArgValues.empty())
5934 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
5935 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00005936 SDB->setValue(I, Res);
5937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005938 // If this argument is live outside of the entry block, insert a copy from
5939 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005940 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005941 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005942 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005943
Dan Gohman98ca4f22009-08-05 01:29:28 +00005944 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945
5946 // Finally, if the target has anything special to do, allow it to do so.
5947 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00005948 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005949}
5950
5951/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5952/// ensure constants are generated when needed. Remember the virtual registers
5953/// that need to be added to the Machine PHI nodes as input. We cannot just
5954/// directly add them, because expansion might result in multiple MBB's for one
5955/// BB. As such, the start of the BB might correspond to a different MBB than
5956/// the end.
5957///
5958void
Dan Gohman46510a72010-04-15 01:51:59 +00005959SelectionDAGISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
5960 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005961
5962 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5963
5964 // Check successor nodes' PHI nodes that expect a constant to be available
5965 // from this block.
5966 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00005967 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005968 if (!isa<PHINode>(SuccBB->begin())) continue;
5969 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005970
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005971 // If this terminator has multiple identical successors (common for
5972 // switches), only handle each succ once.
5973 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005975 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976
5977 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5978 // nodes and Machine PHI nodes, but the incoming operands have not been
5979 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00005980 for (BasicBlock::const_iterator I = SuccBB->begin();
5981 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005982 // Ignore dead phi's.
5983 if (PN->use_empty()) continue;
5984
5985 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00005986 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987
Dan Gohman46510a72010-04-15 01:51:59 +00005988 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005989 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005990 if (RegOut == 0) {
5991 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005992 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005993 }
5994 Reg = RegOut;
5995 } else {
5996 Reg = FuncInfo->ValueMap[PHIOp];
5997 if (Reg == 0) {
5998 assert(isa<AllocaInst>(PHIOp) &&
5999 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6000 "Didn't codegen value into a register!??");
6001 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006002 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 }
6004 }
6005
6006 // Remember that this register needs to added to the machine PHI node as
6007 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006008 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6010 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006011 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006012 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006013 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006014 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006015 Reg += NumRegisters;
6016 }
6017 }
6018 }
Dan Gohman2048b852009-11-23 18:04:58 +00006019 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020}
6021
Dan Gohman3df24e62008-09-03 23:12:08 +00006022/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6023/// supports legal types, and it emits MachineInstrs directly instead of
6024/// creating SelectionDAG nodes.
6025///
6026bool
Dan Gohman46510a72010-04-15 01:51:59 +00006027SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(const BasicBlock *LLVMBB,
Dan Gohman3df24e62008-09-03 23:12:08 +00006028 FastISel *F) {
Dan Gohman46510a72010-04-15 01:51:59 +00006029 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006030
Dan Gohman3df24e62008-09-03 23:12:08 +00006031 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006032 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006033
6034 // Check successor nodes' PHI nodes that expect a constant to be available
6035 // from this block.
6036 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006037 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohman3df24e62008-09-03 23:12:08 +00006038 if (!isa<PHINode>(SuccBB->begin())) continue;
6039 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006040
Dan Gohman3df24e62008-09-03 23:12:08 +00006041 // If this terminator has multiple identical successors (common for
6042 // switches), only handle each succ once.
6043 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006044
Dan Gohman3df24e62008-09-03 23:12:08 +00006045 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohman3df24e62008-09-03 23:12:08 +00006046
6047 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6048 // nodes and Machine PHI nodes, but the incoming operands have not been
6049 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006050 for (BasicBlock::const_iterator I = SuccBB->begin();
6051 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohman3df24e62008-09-03 23:12:08 +00006052 // Ignore dead phi's.
6053 if (PN->use_empty()) continue;
6054
6055 // Only handle legal types. Two interesting things to note here. First,
6056 // by bailing out early, we may leave behind some dead instructions,
6057 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6058 // own moves. Second, this check is necessary becuase FastISel doesn't
6059 // use CreateRegForValue to create registers, so it always creates
6060 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006061 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006062 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6063 // Promote MVT::i1.
6064 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006065 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006066 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006067 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006068 return false;
6069 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006070 }
6071
Dan Gohman46510a72010-04-15 01:51:59 +00006072 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohman3df24e62008-09-03 23:12:08 +00006073
6074 unsigned Reg = F->getRegForValue(PHIOp);
6075 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006076 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006077 return false;
6078 }
Dan Gohman2048b852009-11-23 18:04:58 +00006079 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006080 }
6081 }
6082
6083 return true;
6084}