blob: 695b7913b4895166138a5e9136061b3fc1113f53 [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 Gohman5eb6d652010-04-21 01:22:34 +000033#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000034#include "llvm/CodeGen/FastISel.h"
35#include "llvm/CodeGen/GCStrategy.h"
36#include "llvm/CodeGen/GCMetadata.h"
37#include "llvm/CodeGen/MachineFunction.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
40#include "llvm/CodeGen/MachineJumpTableInfo.h"
41#include "llvm/CodeGen/MachineModuleInfo.h"
42#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000043#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000045#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000046#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetData.h"
48#include "llvm/Target/TargetFrameInfo.h"
49#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000050#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000052#include "llvm/Target/TargetOptions.h"
53#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000054#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000056#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000058#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include <algorithm>
60using namespace llvm;
61
Dale Johannesen601d3c02008-09-05 01:48:15 +000062/// LimitFloatPrecision - Generate low-precision inline sequences for
63/// some float libcalls (6, 8 or 12 bits).
64static unsigned LimitFloatPrecision;
65
66static cl::opt<unsigned, true>
67LimitFPPrecision("limit-float-precision",
68 cl::desc("Generate low-precision inline sequences "
69 "for some float libcalls"),
70 cl::location(LimitFloatPrecision),
71 cl::init(0));
72
Dan Gohmanf9bd4502009-11-23 17:46:23 +000073namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000074 /// RegsForValue - This struct represents the registers (physical or virtual)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +000075 /// that a particular set of values is assigned, and the type information
76 /// about the value. The most common situation is to represent one value at a
77 /// time, but struct or array values are handled element-wise as multiple
78 /// values. The splitting of aggregates is performed recursively, so that we
79 /// never have aggregate-typed registers. The values at this point do not
80 /// necessarily have legal types, so each value may require one or more
81 /// registers of some legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000082 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000083 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000084 /// TLI - The TargetLowering object.
85 ///
86 const TargetLowering *TLI;
87
88 /// ValueVTs - The value types of the values, which may not be legal, and
89 /// may need be promoted or synthesized from one or more registers.
90 ///
Owen Andersone50ed302009-08-10 22:56:29 +000091 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000092
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000093 /// RegVTs - The value types of the registers. This is the same size as
94 /// ValueVTs and it records, for each value, what the type of the assigned
95 /// register or registers are. (Individual values are never synthesized
96 /// from more than one type of register.)
97 ///
98 /// With virtual registers, the contents of RegVTs is redundant with TLI's
99 /// getRegisterType member function, however when with physical registers
100 /// it is necessary to have a separate record of the types.
101 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000102 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000104 /// Regs - This list holds the registers assigned to the values.
105 /// Each legal or promoted value requires one register, and each
106 /// expanded value requires multiple registers.
107 ///
108 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000112 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000113 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000114 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000115 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
116 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000117 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000118 const SmallVector<EVT, 4> &regvts,
119 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000121 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000122 unsigned Reg, const Type *Ty) : TLI(&tli) {
123 ComputeValueVTs(tli, Ty, ValueVTs);
124
125 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000126 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000127 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
128 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 for (unsigned i = 0; i != NumRegs; ++i)
130 Regs.push_back(Reg + i);
131 RegVTs.push_back(RegisterVT);
132 Reg += NumRegs;
133 }
134 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000135
Evan Cheng8112b532010-02-10 01:21:02 +0000136 /// areValueTypesLegal - Return true if types of all the values are legal.
137 bool areValueTypesLegal() {
138 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
139 EVT RegisterVT = RegVTs[Value];
140 if (!TLI->isTypeLegal(RegisterVT))
141 return false;
142 }
143 return true;
144 }
145
146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000147 /// append - Add the specified values to this one.
148 void append(const RegsForValue &RHS) {
149 TLI = RHS.TLI;
150 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
151 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
152 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
153 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000154
155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000156 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000157 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 /// Chain/Flag as the input and updates them for the output Chain/Flag.
159 /// If the Flag pointer is NULL, no flag is used.
Bill Wendling46ada192010-03-02 01:55:18 +0000160 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +0000161 SDValue &Chain, SDValue *Flag) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000162
163 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000164 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000165 /// Chain/Flag as the input and updates them for the output Chain/Flag.
166 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000167 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +0000168 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000170 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000171 /// operand list. This adds the code marker, matching input operand index
172 /// (if applicable), and includes the number of values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +0000173 void AddInlineAsmOperands(unsigned Kind,
Evan Cheng697cbbf2009-03-20 18:03:34 +0000174 bool HasMatching, unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +0000175 SelectionDAG &DAG,
Bill Wendling651ad132009-12-22 01:25:10 +0000176 std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000177 };
178}
179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000180/// getCopyFromParts - Create a value that contains the specified legal parts
181/// combined into the value they represent. If the parts combine to a type
182/// larger then ValueVT then AssertOp can be used to specify whether the extra
183/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
184/// (ISD::AssertSext).
Bill Wendling46ada192010-03-02 01:55:18 +0000185static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000186 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000187 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000188 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000189 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000190 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 SDValue Val = Parts[0];
192
193 if (NumParts > 1) {
194 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000195 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 unsigned PartBits = PartVT.getSizeInBits();
197 unsigned ValueBits = ValueVT.getSizeInBits();
198
199 // Assemble the power of 2 part.
200 unsigned RoundParts = NumParts & (NumParts - 1) ?
201 1 << Log2_32(NumParts) : NumParts;
202 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000203 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000204 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000205 SDValue Lo, Hi;
206
Owen Anderson23b9b192009-08-12 00:36:31 +0000207 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000209 if (RoundParts > 2) {
Bill Wendling46ada192010-03-02 01:55:18 +0000210 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000211 PartVT, HalfVT);
Bill Wendling46ada192010-03-02 01:55:18 +0000212 Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000213 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000214 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000215 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
216 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000217 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000218
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000219 if (TLI.isBigEndian())
220 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000221
Dale Johannesen66978ee2009-01-31 02:22:37 +0000222 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000223
224 if (RoundParts < NumParts) {
225 // Assemble the trailing non-power-of-2 part.
226 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000227 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling46ada192010-03-02 01:55:18 +0000228 Hi = getCopyFromParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000229 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000230
231 // Combine the round and odd parts.
232 Lo = Val;
233 if (TLI.isBigEndian())
234 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000235 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000236 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
237 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000238 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000239 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000240 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
241 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000242 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000243 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000244 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000245 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000246 unsigned NumIntermediates;
247 unsigned NumRegs =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000248 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000249 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000250 assert(NumRegs == NumParts
251 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000252 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000253 assert(RegisterVT == PartVT
254 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 assert(RegisterVT == Parts[0].getValueType() &&
256 "Part type doesn't match part!");
257
258 // Assemble the parts into intermediate operands.
259 SmallVector<SDValue, 8> Ops(NumIntermediates);
260 if (NumIntermediates == NumParts) {
261 // If the register was not expanded, truncate or copy the value,
262 // as appropriate.
263 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000264 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000265 PartVT, IntermediateVT);
266 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000267 // If the intermediate type was expanded, build the intermediate
268 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000269 assert(NumParts % NumIntermediates == 0 &&
270 "Must expand into a divisible number of parts!");
271 unsigned Factor = NumParts / NumIntermediates;
272 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000273 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000274 PartVT, IntermediateVT);
275 }
276
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000277 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
278 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000280 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000281 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000282 } else if (PartVT.isFloatingPoint()) {
283 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000285 "Unexpected split");
286 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
288 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000289 if (TLI.isBigEndian())
290 std::swap(Lo, Hi);
291 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
292 } else {
293 // FP split into integer parts (soft fp)
294 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
295 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000296 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling46ada192010-03-02 01:55:18 +0000297 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000298 }
299 }
300
301 // There is now one part, held in Val. Correct it to match ValueVT.
302 PartVT = Val.getValueType();
303
304 if (PartVT == ValueVT)
305 return Val;
306
307 if (PartVT.isVector()) {
308 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000309 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000310 }
311
312 if (ValueVT.isVector()) {
313 assert(ValueVT.getVectorElementType() == PartVT &&
314 ValueVT.getVectorNumElements() == 1 &&
315 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000316 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000317 }
318
319 if (PartVT.isInteger() &&
320 ValueVT.isInteger()) {
321 if (ValueVT.bitsLT(PartVT)) {
322 // For a truncate, see if we have any information to
323 // indicate whether the truncated bits will always be
324 // zero or sign-extension.
325 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000326 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000327 DAG.getValueType(ValueVT));
Bill Wendling4533cac2010-01-28 21:51:40 +0000328 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000329 } else {
Bill Wendling4533cac2010-01-28 21:51:40 +0000330 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000331 }
332 }
333
334 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000335 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000336 // FP_ROUND's are always exact here.
Bill Wendling4533cac2010-01-28 21:51:40 +0000337 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
338 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000339 }
340
Bill Wendling4533cac2010-01-28 21:51:40 +0000341 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000342 }
343
Bill Wendling4533cac2010-01-28 21:51:40 +0000344 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
345 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000346
Torok Edwinc23197a2009-07-14 16:55:14 +0000347 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000348 return SDValue();
349}
350
351/// getCopyToParts - Create a series of nodes that contain the specified value
352/// split into legal parts. If the parts contain more bits than Val, then, for
353/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling46ada192010-03-02 01:55:18 +0000354static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000355 SDValue Val, SDValue *Parts, unsigned NumParts,
356 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000357 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000358 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000359 EVT PtrVT = TLI.getPointerTy();
360 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000362 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000363 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
364
365 if (!NumParts)
366 return;
367
368 if (!ValueVT.isVector()) {
369 if (PartVT == ValueVT) {
370 assert(NumParts == 1 && "No-op copy with multiple parts!");
371 Parts[0] = Val;
372 return;
373 }
374
375 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
376 // If the parts cover more bits than the value has, promote the value.
377 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
378 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000379 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000380 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000381 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000382 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000383 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000384 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000385 }
386 } else if (PartBits == ValueVT.getSizeInBits()) {
387 // Different types of the same size.
388 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000389 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000390 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
391 // If the parts cover less bits than value has, truncate the value.
392 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000393 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000394 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000395 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000396 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000397 }
398 }
399
400 // The value may have changed - recompute ValueVT.
401 ValueVT = Val.getValueType();
402 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
403 "Failed to tile the value with PartVT!");
404
405 if (NumParts == 1) {
406 assert(PartVT == ValueVT && "Type conversion failed!");
407 Parts[0] = Val;
408 return;
409 }
410
411 // Expand the value into multiple parts.
412 if (NumParts & (NumParts - 1)) {
413 // The number of parts is not a power of 2. Split off and copy the tail.
414 assert(PartVT.isInteger() && ValueVT.isInteger() &&
415 "Do not know what to expand to!");
416 unsigned RoundParts = 1 << Log2_32(NumParts);
417 unsigned RoundBits = RoundParts * PartBits;
418 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000419 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000420 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000421 TLI.getPointerTy()));
Bill Wendling46ada192010-03-02 01:55:18 +0000422 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000423 OddParts, PartVT);
424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000425 if (TLI.isBigEndian())
426 // The odd parts were reversed by getCopyToParts - unreverse them.
427 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000429 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000430 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000431 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000432 }
433
434 // The number of parts is a power of 2. Repeatedly bisect the value using
435 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000436 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000437 EVT::getIntegerVT(*DAG.getContext(),
438 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000439 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000441 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
442 for (unsigned i = 0; i < NumParts; i += StepSize) {
443 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000444 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000445 SDValue &Part0 = Parts[i];
446 SDValue &Part1 = Parts[i+StepSize/2];
447
Scott Michelfdc40a02009-02-17 22:15:04 +0000448 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000449 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000450 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000451 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000452 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000453 DAG.getConstant(0, PtrVT));
454
455 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000456 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000457 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000458 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000459 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000460 }
461 }
462 }
463
464 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000465 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466
467 return;
468 }
469
470 // Vector ValueVT.
471 if (NumParts == 1) {
472 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000473 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000474 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000475 } else {
476 assert(ValueVT.getVectorElementType() == PartVT &&
477 ValueVT.getVectorNumElements() == 1 &&
478 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000479 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000480 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000481 DAG.getConstant(0, PtrVT));
482 }
483 }
484
485 Parts[0] = Val;
486 return;
487 }
488
489 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000490 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000491 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000492 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
493 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000494 unsigned NumElements = ValueVT.getVectorNumElements();
495
496 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
497 NumParts = NumRegs; // Silence a compiler warning.
498 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
499
500 // Split the vector into intermediate operands.
501 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000502 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000503 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000504 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505 IntermediateVT, Val,
506 DAG.getConstant(i * (NumElements / NumIntermediates),
507 PtrVT));
508 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000509 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000510 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000511 DAG.getConstant(i, PtrVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000512 }
513
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000514 // Split the intermediate operands into legal parts.
515 if (NumParts == NumIntermediates) {
516 // If the register was not expanded, promote or copy the value,
517 // as appropriate.
518 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000519 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000520 } else if (NumParts > 0) {
521 // If the intermediate type was expanded, split each the value into
522 // legal parts.
523 assert(NumParts % NumIntermediates == 0 &&
524 "Must expand into a divisible number of parts!");
525 unsigned Factor = NumParts / NumIntermediates;
526 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000527 getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000528 }
529}
530
531
Dan Gohman2048b852009-11-23 18:04:58 +0000532void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 AA = &aa;
534 GFI = gfi;
535 TD = DAG.getTarget().getTargetData();
536}
537
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000538/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000539/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000540/// for a new block. This doesn't clear out information about
541/// additional blocks that are needed to complete switch lowering
542/// or PHI node updating; that information is cleared out as it is
543/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000544void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 NodeMap.clear();
546 PendingLoads.clear();
547 PendingExports.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000548 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000549 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000550}
551
552/// getRoot - Return the current virtual root of the Selection DAG,
553/// flushing any PendingLoad items. This must be done before emitting
554/// a store or any other node that may need to be ordered after any
555/// prior load instructions.
556///
Dan Gohman2048b852009-11-23 18:04:58 +0000557SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000558 if (PendingLoads.empty())
559 return DAG.getRoot();
560
561 if (PendingLoads.size() == 1) {
562 SDValue Root = PendingLoads[0];
563 DAG.setRoot(Root);
564 PendingLoads.clear();
565 return Root;
566 }
567
568 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000570 &PendingLoads[0], PendingLoads.size());
571 PendingLoads.clear();
572 DAG.setRoot(Root);
573 return Root;
574}
575
576/// getControlRoot - Similar to getRoot, but instead of flushing all the
577/// PendingLoad items, flush all the PendingExports items. It is necessary
578/// to do this before emitting a terminator instruction.
579///
Dan Gohman2048b852009-11-23 18:04:58 +0000580SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000581 SDValue Root = DAG.getRoot();
582
583 if (PendingExports.empty())
584 return Root;
585
586 // Turn all of the CopyToReg chains into one factored node.
587 if (Root.getOpcode() != ISD::EntryToken) {
588 unsigned i = 0, e = PendingExports.size();
589 for (; i != e; ++i) {
590 assert(PendingExports[i].getNode()->getNumOperands() > 1);
591 if (PendingExports[i].getNode()->getOperand(0) == Root)
592 break; // Don't add the root if we already indirectly depend on it.
593 }
594
595 if (i == e)
596 PendingExports.push_back(Root);
597 }
598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000600 &PendingExports[0],
601 PendingExports.size());
602 PendingExports.clear();
603 DAG.setRoot(Root);
604 return Root;
605}
606
Bill Wendling4533cac2010-01-28 21:51:40 +0000607void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
608 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
609 DAG.AssignOrdering(Node, SDNodeOrder);
610
611 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
612 AssignOrderingToNode(Node->getOperand(I).getNode());
613}
614
Dan Gohman46510a72010-04-15 01:51:59 +0000615void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000616 // Set up outgoing PHI node register values before emitting the terminator.
617 if (isa<TerminatorInst>(&I))
618 HandlePHINodesInSuccessorBlocks(I.getParent());
619
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000620 CurDebugLoc = I.getDebugLoc();
621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000622 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000623
Dan Gohman92884f72010-04-20 15:03:56 +0000624 if (!isa<TerminatorInst>(&I) && !HasTailCall)
625 CopyToExportRegsIfNeeded(&I);
626
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000627 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000628}
629
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000630void SelectionDAGBuilder::visitPHI(const PHINode &) {
631 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
632}
633
Dan Gohman46510a72010-04-15 01:51:59 +0000634void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000635 // Note: this doesn't use InstVisitor, because it has to work with
636 // ConstantExpr's in addition to instructions.
637 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000638 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000639 // Build the switch statement using the Instruction.def file.
640#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000641 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000642#include "llvm/Instruction.def"
643 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000644
645 // Assign the ordering to the freshly created DAG nodes.
646 if (NodeMap.count(&I)) {
647 ++SDNodeOrder;
648 AssignOrderingToNode(getValue(&I).getNode());
649 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000650}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000651
Dan Gohman2048b852009-11-23 18:04:58 +0000652SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000653 SDValue &N = NodeMap[V];
654 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000655
Dan Gohman383b5f62010-04-17 15:32:28 +0000656 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000657 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000658
Dan Gohman383b5f62010-04-17 15:32:28 +0000659 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000660 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000661
Dan Gohman383b5f62010-04-17 15:32:28 +0000662 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000664
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000665 if (isa<ConstantPointerNull>(C))
666 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000667
Dan Gohman383b5f62010-04-17 15:32:28 +0000668 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000669 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000670
Nate Begeman9008ca62009-04-27 18:41:29 +0000671 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000672 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673
Dan Gohman383b5f62010-04-17 15:32:28 +0000674 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675 visit(CE->getOpcode(), *CE);
676 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000677 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000678 return N1;
679 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000681 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
682 SmallVector<SDValue, 4> Constants;
683 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
684 OI != OE; ++OI) {
685 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000686 // If the operand is an empty aggregate, there are no values.
687 if (!Val) continue;
688 // Add each leaf value from the operand to the Constants list
689 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000690 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
691 Constants.push_back(SDValue(Val, i));
692 }
Bill Wendling87710f02009-12-21 23:47:40 +0000693
Bill Wendling4533cac2010-01-28 21:51:40 +0000694 return DAG.getMergeValues(&Constants[0], Constants.size(),
695 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000696 }
697
Duncan Sands1df98592010-02-16 11:11:14 +0000698 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000699 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
700 "Unknown struct or array constant!");
701
Owen Andersone50ed302009-08-10 22:56:29 +0000702 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000703 ComputeValueVTs(TLI, C->getType(), ValueVTs);
704 unsigned NumElts = ValueVTs.size();
705 if (NumElts == 0)
706 return SDValue(); // empty struct
707 SmallVector<SDValue, 4> Constants(NumElts);
708 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000709 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000710 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000711 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000712 else if (EltVT.isFloatingPoint())
713 Constants[i] = DAG.getConstantFP(0, EltVT);
714 else
715 Constants[i] = DAG.getConstant(0, EltVT);
716 }
Bill Wendling87710f02009-12-21 23:47:40 +0000717
Bill Wendling4533cac2010-01-28 21:51:40 +0000718 return DAG.getMergeValues(&Constants[0], NumElts,
719 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000720 }
721
Dan Gohman383b5f62010-04-17 15:32:28 +0000722 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000723 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000724
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000725 const VectorType *VecTy = cast<VectorType>(V->getType());
726 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000727
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000728 // Now that we know the number and type of the elements, get that number of
729 // elements into the Ops array based on what kind of constant it is.
730 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000731 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000732 for (unsigned i = 0; i != NumElements; ++i)
733 Ops.push_back(getValue(CP->getOperand(i)));
734 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000735 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000736 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737
738 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000739 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000740 Op = DAG.getConstantFP(0, EltVT);
741 else
742 Op = DAG.getConstant(0, EltVT);
743 Ops.assign(NumElements, Op);
744 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000746 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000747 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
748 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000749 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000750
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000751 // If this is a static alloca, generate it as the frameindex instead of
752 // computation.
753 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
754 DenseMap<const AllocaInst*, int>::iterator SI =
755 FuncInfo.StaticAllocaMap.find(AI);
756 if (SI != FuncInfo.StaticAllocaMap.end())
757 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
758 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000759
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000760 unsigned InReg = FuncInfo.ValueMap[V];
761 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000762
Owen Anderson23b9b192009-08-12 00:36:31 +0000763 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000765 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000766}
767
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000768/// Get the EVTs and ArgFlags collections that represent the legalized return
769/// type of the given function. This does not require a DAG or a return value,
770/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000771static void getReturnInfo(const Type* ReturnType,
772 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000773 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000774 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000775 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000776 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000777 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000778 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000779 if (NumValues == 0) return;
780 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000781
782 for (unsigned j = 0, f = NumValues; j != f; ++j) {
783 EVT VT = ValueVTs[j];
784 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000785
786 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000787 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000788 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000789 ExtendKind = ISD::ZERO_EXTEND;
790
791 // FIXME: C calling convention requires the return type to be promoted to
792 // at least 32-bit. But this is not necessary for non-C calling
793 // conventions. The frontend should mark functions whose return values
794 // require promoting with signext or zeroext attributes.
795 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000796 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000797 if (VT.bitsLT(MinVT))
798 VT = MinVT;
799 }
800
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000801 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
802 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000803 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
804 PartVT.getTypeForEVT(ReturnType->getContext()));
805
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000806 // 'inreg' on function refers to return value
807 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000808 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000809 Flags.setInReg();
810
811 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000812 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000813 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000814 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000815 Flags.setZExt();
816
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000817 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000818 OutVTs.push_back(PartVT);
819 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000820 if (Offsets)
821 {
822 Offsets->push_back(Offset);
823 Offset += PartSize;
824 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000825 }
826 }
827}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000828
Dan Gohman46510a72010-04-15 01:51:59 +0000829void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000830 SDValue Chain = getControlRoot();
831 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000832 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000833
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000834 if (!FLI.CanLowerReturn) {
835 unsigned DemoteReg = FLI.DemoteRegister;
836 const Function *F = I.getParent()->getParent();
837
838 // Emit a store of the return value through the virtual register.
839 // Leave Outs empty so that LowerReturn won't try to load return
840 // registers the usual way.
841 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000842 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000843 PtrValueVTs);
844
845 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
846 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000847
Owen Andersone50ed302009-08-10 22:56:29 +0000848 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000849 SmallVector<uint64_t, 4> Offsets;
850 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000851 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000852
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000853 SmallVector<SDValue, 4> Chains(NumValues);
854 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000855 for (unsigned i = 0; i != NumValues; ++i) {
856 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
857 DAG.getConstant(Offsets[i], PtrVT));
858 Chains[i] =
859 DAG.getStore(Chain, getCurDebugLoc(),
860 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000861 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000862 }
863
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000864 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
865 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000866 } else if (I.getNumOperands() != 0) {
867 SmallVector<EVT, 4> ValueVTs;
868 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
869 unsigned NumValues = ValueVTs.size();
870 if (NumValues) {
871 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000872 for (unsigned j = 0, f = NumValues; j != f; ++j) {
873 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000874
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000875 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000876
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000877 const Function *F = I.getParent()->getParent();
878 if (F->paramHasAttr(0, Attribute::SExt))
879 ExtendKind = ISD::SIGN_EXTEND;
880 else if (F->paramHasAttr(0, Attribute::ZExt))
881 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000883 // FIXME: C calling convention requires the return type to be promoted
884 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000885 // conventions. The frontend should mark functions whose return values
886 // require promoting with signext or zeroext attributes.
887 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
888 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
889 if (VT.bitsLT(MinVT))
890 VT = MinVT;
891 }
892
893 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
894 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
895 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000896 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000897 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
898 &Parts[0], NumParts, PartVT, ExtendKind);
899
900 // 'inreg' on function refers to return value
901 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
902 if (F->paramHasAttr(0, Attribute::InReg))
903 Flags.setInReg();
904
905 // Propagate extension type if any
906 if (F->paramHasAttr(0, Attribute::SExt))
907 Flags.setSExt();
908 else if (F->paramHasAttr(0, Attribute::ZExt))
909 Flags.setZExt();
910
911 for (unsigned i = 0; i < NumParts; ++i)
912 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000913 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000914 }
915 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000916
917 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000918 CallingConv::ID CallConv =
919 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000920 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
921 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000922
923 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000925 "LowerReturn didn't return a valid chain!");
926
927 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000928 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000929}
930
Dan Gohmanad62f532009-04-23 23:13:24 +0000931/// CopyToExportRegsIfNeeded - If the given value has virtual registers
932/// created for it, emit nodes to copy the value into the virtual
933/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000934void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000935 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
936 if (VMI != FuncInfo.ValueMap.end()) {
937 assert(!V->use_empty() && "Unused value assigned virtual registers!");
938 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000939 }
940}
941
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000942/// ExportFromCurrentBlock - If this condition isn't known to be exported from
943/// the current basic block, add it to ValueMap now so that we'll get a
944/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000945void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 // No need to export constants.
947 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000948
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000949 // Already exported?
950 if (FuncInfo.isExportedInst(V)) return;
951
952 unsigned Reg = FuncInfo.InitializeRegForValue(V);
953 CopyValueToVirtualRegister(V, Reg);
954}
955
Dan Gohman46510a72010-04-15 01:51:59 +0000956bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000957 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000958 // The operands of the setcc have to be in this block. We don't know
959 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000960 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000961 // Can export from current BB.
962 if (VI->getParent() == FromBB)
963 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965 // Is already exported, noop.
966 return FuncInfo.isExportedInst(V);
967 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000969 // If this is an argument, we can export it if the BB is the entry block or
970 // if it is already exported.
971 if (isa<Argument>(V)) {
972 if (FromBB == &FromBB->getParent()->getEntryBlock())
973 return true;
974
975 // Otherwise, can only export this if it is already exported.
976 return FuncInfo.isExportedInst(V);
977 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000979 // Otherwise, constants can always be exported.
980 return true;
981}
982
983static bool InBlock(const Value *V, const BasicBlock *BB) {
984 if (const Instruction *I = dyn_cast<Instruction>(V))
985 return I->getParent() == BB;
986 return true;
987}
988
Dan Gohmanc2277342008-10-17 21:16:08 +0000989/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
990/// This function emits a branch and is used at the leaves of an OR or an
991/// AND operator tree.
992///
993void
Dan Gohman46510a72010-04-15 01:51:59 +0000994SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000995 MachineBasicBlock *TBB,
996 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000997 MachineBasicBlock *CurBB,
998 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000999 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001000
Dan Gohmanc2277342008-10-17 21:16:08 +00001001 // If the leaf of the tree is a comparison, merge the condition into
1002 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001003 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001004 // The operands of the cmp have to be in this block. We don't know
1005 // how to export them from some other block. If this is the first block
1006 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001007 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001008 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1009 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001010 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001011 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001012 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001013 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001014 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 } else {
1016 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001017 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001018 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001019
1020 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001021 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1022 SwitchCases.push_back(CB);
1023 return;
1024 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001025 }
1026
1027 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001028 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001029 NULL, TBB, FBB, CurBB);
1030 SwitchCases.push_back(CB);
1031}
1032
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001033/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001034void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001035 MachineBasicBlock *TBB,
1036 MachineBasicBlock *FBB,
1037 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001038 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001039 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001040 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001041 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001042 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001043 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1044 BOp->getParent() != CurBB->getBasicBlock() ||
1045 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1046 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001047 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001048 return;
1049 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001051 // Create TmpBB after CurBB.
1052 MachineFunction::iterator BBI = CurBB;
1053 MachineFunction &MF = DAG.getMachineFunction();
1054 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1055 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001057 if (Opc == Instruction::Or) {
1058 // Codegen X | Y as:
1059 // jmp_if_X TBB
1060 // jmp TmpBB
1061 // TmpBB:
1062 // jmp_if_Y TBB
1063 // jmp FBB
1064 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001065
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001067 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001069 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001070 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071 } else {
1072 assert(Opc == Instruction::And && "Unknown merge op!");
1073 // Codegen X & Y as:
1074 // jmp_if_X TmpBB
1075 // jmp FBB
1076 // TmpBB:
1077 // jmp_if_Y TBB
1078 // jmp FBB
1079 //
1080 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001083 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001084
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001085 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001086 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 }
1088}
1089
1090/// If the set of cases should be emitted as a series of branches, return true.
1091/// If we should emit this as a bunch of and/or'd together conditions, return
1092/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001093bool
Dan Gohman2048b852009-11-23 18:04:58 +00001094SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001097 // If this is two comparisons of the same values or'd or and'd together, they
1098 // will get folded into a single comparison, so don't emit two blocks.
1099 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1100 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1101 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1102 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1103 return false;
1104 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001105
Chris Lattner133ce872010-01-02 00:00:03 +00001106 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1107 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1108 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1109 Cases[0].CC == Cases[1].CC &&
1110 isa<Constant>(Cases[0].CmpRHS) &&
1111 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1112 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1113 return false;
1114 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1115 return false;
1116 }
1117
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001118 return true;
1119}
1120
Dan Gohman46510a72010-04-15 01:51:59 +00001121void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001122 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001124 // Update machine-CFG edges.
1125 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1126
1127 // Figure out which block is immediately after the current one.
1128 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001129 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001130 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001131 NextBlock = BBI;
1132
1133 if (I.isUnconditional()) {
1134 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001135 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001136
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001137 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001138 if (Succ0MBB != NextBlock)
1139 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001140 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001141 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001143 return;
1144 }
1145
1146 // If this condition is one of the special cases we handle, do special stuff
1147 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001148 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001149 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1150
1151 // If this is a series of conditions that are or'd or and'd together, emit
1152 // this as a sequence of branches instead of setcc's with and/or operations.
1153 // For example, instead of something like:
1154 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001155 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001156 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // or C, F
1159 // jnz foo
1160 // Emit:
1161 // cmp A, B
1162 // je foo
1163 // cmp D, E
1164 // jle foo
1165 //
Dan Gohman46510a72010-04-15 01:51:59 +00001166 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001167 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001168 (BOp->getOpcode() == Instruction::And ||
1169 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001170 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1171 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172 // If the compares in later blocks need to use values not currently
1173 // exported from this block, export them now. This block should always
1174 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001175 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001176
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001177 // Allow some cases to be rejected.
1178 if (ShouldEmitAsBranches(SwitchCases)) {
1179 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1180 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1181 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1182 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001184 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001185 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001186 SwitchCases.erase(SwitchCases.begin());
1187 return;
1188 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001189
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001190 // Okay, we decided not to do this, remove any inserted MBB's and clear
1191 // SwitchCases.
1192 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001193 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195 SwitchCases.clear();
1196 }
1197 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001199 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001200 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001201 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001202
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203 // Use visitSwitchCase to actually insert the fast branch sequence for this
1204 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001205 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001206}
1207
1208/// visitSwitchCase - Emits the necessary code to represent a single node in
1209/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001210void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1211 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 SDValue Cond;
1213 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001214 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001215
1216 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001217 if (CB.CmpMHS == NULL) {
1218 // Fold "(X == true)" to X and "(X == false)" to !X to
1219 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001220 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001221 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001222 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001223 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001224 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001225 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001226 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 } else {
1230 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1231
Anton Korobeynikov23218582008-12-23 22:25:27 +00001232 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1233 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001234
1235 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001236 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001237
1238 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001240 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001242 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001243 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001244 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001245 DAG.getConstant(High-Low, VT), ISD::SETULE);
1246 }
1247 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001248
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001249 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001250 SwitchBB->addSuccessor(CB.TrueBB);
1251 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001253 // Set NextBlock to be the MBB immediately after the current one, if any.
1254 // This is used to avoid emitting unnecessary branches to the next block.
1255 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001256 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001257 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001258 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 // If the lhs block is the next block, invert the condition so that we can
1261 // fall through to the lhs instead of the rhs block.
1262 if (CB.TrueBB == NextBlock) {
1263 std::swap(CB.TrueBB, CB.FalseBB);
1264 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001265 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001267
Dale Johannesenf5d97892009-02-04 01:48:28 +00001268 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001270 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001272 // If the branch was constant folded, fix up the CFG.
1273 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001274 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 } else {
1276 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001277 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001278 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001279
Bill Wendling4533cac2010-01-28 21:51:40 +00001280 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001281 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1282 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001283 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001284
1285 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001286}
1287
1288/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001289void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290 // Emit the code for the jump table
1291 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001292 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001293 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1294 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001296 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1297 MVT::Other, Index.getValue(1),
1298 Table, Index);
1299 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001300}
1301
1302/// visitJumpTableHeader - This function emits necessary code to produce index
1303/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001304void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001305 JumpTableHeader &JTH,
1306 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001307 // Subtract the lowest switch case value from the value being switched on and
1308 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309 // difference between smallest and largest cases.
1310 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001311 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001312 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001313 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001314
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001315 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001316 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001317 // can be used as an index into the jump table in a subsequent basic block.
1318 // This value may be smaller or larger than the target's pointer type, and
1319 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001320 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001322 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001323 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1324 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325 JT.Reg = JumpTableReg;
1326
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001327 // Emit the range check for the jump table, and branch to the default block
1328 // for the switch statement if the value being switched on exceeds the largest
1329 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001330 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001331 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001332 DAG.getConstant(JTH.Last-JTH.First,VT),
1333 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334
1335 // Set NextBlock to be the MBB immediately after the current one, if any.
1336 // This is used to avoid emitting unnecessary branches to the next block.
1337 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001338 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001339
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001340 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001341 NextBlock = BBI;
1342
Dale Johannesen66978ee2009-01-31 02:22:37 +00001343 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001344 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001345 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001346
Bill Wendling4533cac2010-01-28 21:51:40 +00001347 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001348 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1349 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001350
Bill Wendling87710f02009-12-21 23:47:40 +00001351 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001352}
1353
1354/// visitBitTestHeader - This function emits necessary code to produce value
1355/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001356void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1357 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001358 // Subtract the minimum value
1359 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001360 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001361 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001362 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001363
1364 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001365 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001366 TLI.getSetCCResultType(Sub.getValueType()),
1367 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001368 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369
Bill Wendling87710f02009-12-21 23:47:40 +00001370 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1371 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001372
Duncan Sands92abc622009-01-31 15:50:11 +00001373 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001374 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1375 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001376
1377 // Set NextBlock to be the MBB immediately after the current one, if any.
1378 // This is used to avoid emitting unnecessary branches to the next block.
1379 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001380 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001381 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001382 NextBlock = BBI;
1383
1384 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1385
Dan Gohman99be8ae2010-04-19 22:41:47 +00001386 SwitchBB->addSuccessor(B.Default);
1387 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001388
Dale Johannesen66978ee2009-01-31 02:22:37 +00001389 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001391 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001392
Bill Wendling4533cac2010-01-28 21:51:40 +00001393 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001394 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1395 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001396
Bill Wendling87710f02009-12-21 23:47:40 +00001397 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001398}
1399
1400/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001401void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1402 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001403 BitTestCase &B,
1404 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001405 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001406 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001407 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001408 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001409 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001410 DAG.getConstant(1, TLI.getPointerTy()),
1411 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001412
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001413 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001414 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001415 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001416 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001417 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1418 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001419 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001420 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421
Dan Gohman99be8ae2010-04-19 22:41:47 +00001422 SwitchBB->addSuccessor(B.TargetBB);
1423 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001424
Dale Johannesen66978ee2009-01-31 02:22:37 +00001425 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001427 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001428
1429 // Set NextBlock to be the MBB immediately after the current one, if any.
1430 // This is used to avoid emitting unnecessary branches to the next block.
1431 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001432 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001433 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001434 NextBlock = BBI;
1435
Bill Wendling4533cac2010-01-28 21:51:40 +00001436 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001437 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1438 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001439
Bill Wendling87710f02009-12-21 23:47:40 +00001440 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441}
1442
Dan Gohman46510a72010-04-15 01:51:59 +00001443void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001444 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1445
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446 // Retrieve successors.
1447 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1448 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1449
Gabor Greifb67e6b32009-01-15 11:10:44 +00001450 const Value *Callee(I.getCalledValue());
1451 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001452 visitInlineAsm(&I);
1453 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001454 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001455
1456 // If the value of the invoke is used outside of its defining block, make it
1457 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001458 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459
1460 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001461 InvokeMBB->addSuccessor(Return);
1462 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001463
1464 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001465 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1466 MVT::Other, getControlRoot(),
1467 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468}
1469
Dan Gohman46510a72010-04-15 01:51:59 +00001470void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471}
1472
1473/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1474/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001475bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1476 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001477 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001478 MachineBasicBlock *Default,
1479 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001481
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001483 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001484 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001485 return false;
1486
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001487 // Get the MachineFunction which holds the current MBB. This is used when
1488 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001489 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001490
1491 // Figure out which block is immediately after the current one.
1492 MachineBasicBlock *NextBlock = 0;
1493 MachineFunction::iterator BBI = CR.CaseBB;
1494
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001495 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496 NextBlock = BBI;
1497
1498 // TODO: If any two of the cases has the same destination, and if one value
1499 // is the same as the other, but has one bit unset that the other has set,
1500 // use bit manipulation to do two compares at once. For example:
1501 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001502
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001503 // Rearrange the case blocks so that the last one falls through if possible.
1504 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1505 // The last case block won't fall through into 'NextBlock' if we emit the
1506 // branches in this order. See if rearranging a case value would help.
1507 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1508 if (I->BB == NextBlock) {
1509 std::swap(*I, BackCase);
1510 break;
1511 }
1512 }
1513 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 // Create a CaseBlock record representing a conditional branch to
1516 // the Case's target mbb if the value being switched on SV is equal
1517 // to C.
1518 MachineBasicBlock *CurBlock = CR.CaseBB;
1519 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1520 MachineBasicBlock *FallThrough;
1521 if (I != E-1) {
1522 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1523 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001524
1525 // Put SV in a virtual register to make it available from the new blocks.
1526 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001527 } else {
1528 // If the last case doesn't match, go to the default block.
1529 FallThrough = Default;
1530 }
1531
Dan Gohman46510a72010-04-15 01:51:59 +00001532 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001533 ISD::CondCode CC;
1534 if (I->High == I->Low) {
1535 // This is just small small case range :) containing exactly 1 case
1536 CC = ISD::SETEQ;
1537 LHS = SV; RHS = I->High; MHS = NULL;
1538 } else {
1539 CC = ISD::SETLE;
1540 LHS = I->Low; MHS = SV; RHS = I->High;
1541 }
1542 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001543
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001544 // If emitting the first comparison, just call visitSwitchCase to emit the
1545 // code into the current block. Otherwise, push the CaseBlock onto the
1546 // vector to be later processed by SDISel, and insert the node's MBB
1547 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001548 if (CurBlock == SwitchBB)
1549 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 else
1551 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001552
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001553 CurBlock = FallThrough;
1554 }
1555
1556 return true;
1557}
1558
1559static inline bool areJTsAllowed(const TargetLowering &TLI) {
1560 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1562 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001564
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001565static APInt ComputeRange(const APInt &First, const APInt &Last) {
1566 APInt LastExt(Last), FirstExt(First);
1567 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1568 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1569 return (LastExt - FirstExt + 1ULL);
1570}
1571
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001572/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001573bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1574 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001575 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001576 MachineBasicBlock* Default,
1577 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001578 Case& FrontCase = *CR.Range.first;
1579 Case& BackCase = *(CR.Range.second-1);
1580
Chris Lattnere880efe2009-11-07 07:50:34 +00001581 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1582 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001583
Chris Lattnere880efe2009-11-07 07:50:34 +00001584 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1586 I!=E; ++I)
1587 TSize += I->size();
1588
Dan Gohmane0567812010-04-08 23:03:40 +00001589 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001591
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001592 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001593 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001594 if (Density < 0.4)
1595 return false;
1596
David Greene4b69d992010-01-05 01:24:57 +00001597 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001598 << "First entry: " << First << ". Last entry: " << Last << '\n'
1599 << "Range: " << Range
1600 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601
1602 // Get the MachineFunction which holds the current MBB. This is used when
1603 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001604 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605
1606 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001608 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609
1610 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1611
1612 // Create a new basic block to hold the code for loading the address
1613 // of the jump table, and jumping to it. Update successor information;
1614 // we will either branch to the default case for the switch, or the jump
1615 // table.
1616 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1617 CurMF->insert(BBI, JumpTableBB);
1618 CR.CaseBB->addSuccessor(Default);
1619 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001620
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001621 // Build a vector of destination BBs, corresponding to each target
1622 // of the jump table. If the value of the jump table slot corresponds to
1623 // a case statement, push the case's BB onto the vector, otherwise, push
1624 // the default BB.
1625 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001626 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001628 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1629 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001630
1631 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001632 DestBBs.push_back(I->BB);
1633 if (TEI==High)
1634 ++I;
1635 } else {
1636 DestBBs.push_back(Default);
1637 }
1638 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001641 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1642 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 E = DestBBs.end(); I != E; ++I) {
1644 if (!SuccsHandled[(*I)->getNumber()]) {
1645 SuccsHandled[(*I)->getNumber()] = true;
1646 JumpTableBB->addSuccessor(*I);
1647 }
1648 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001649
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001650 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001651 unsigned JTEncoding = TLI.getJumpTableEncoding();
1652 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001653 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 // Set the jump table information so that we can codegen it as a second
1656 // MachineBasicBlock
1657 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001658 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1659 if (CR.CaseBB == SwitchBB)
1660 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001662 JTCases.push_back(JumpTableBlock(JTH, JT));
1663
1664 return true;
1665}
1666
1667/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1668/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001669bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1670 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001671 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001672 MachineBasicBlock *Default,
1673 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001674 // Get the MachineFunction which holds the current MBB. This is used when
1675 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001676 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001677
1678 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001680 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681
1682 Case& FrontCase = *CR.Range.first;
1683 Case& BackCase = *(CR.Range.second-1);
1684 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1685
1686 // Size is the number of Cases represented by this range.
1687 unsigned Size = CR.Range.second - CR.Range.first;
1688
Chris Lattnere880efe2009-11-07 07:50:34 +00001689 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1690 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691 double FMetric = 0;
1692 CaseItr Pivot = CR.Range.first + Size/2;
1693
1694 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1695 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001696 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001697 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1698 I!=E; ++I)
1699 TSize += I->size();
1700
Chris Lattnere880efe2009-11-07 07:50:34 +00001701 APInt LSize = FrontCase.size();
1702 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001703 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001704 << "First: " << First << ", Last: " << Last <<'\n'
1705 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001706 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1707 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001708 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1709 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001710 APInt Range = ComputeRange(LEnd, RBegin);
1711 assert((Range - 2ULL).isNonNegative() &&
1712 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001713 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001714 (LEnd - First + 1ULL).roundToDouble();
1715 double RDensity = (double)RSize.roundToDouble() /
1716 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001717 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001718 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001719 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001720 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1721 << "LDensity: " << LDensity
1722 << ", RDensity: " << RDensity << '\n'
1723 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724 if (FMetric < Metric) {
1725 Pivot = J;
1726 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001727 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001728 }
1729
1730 LSize += J->size();
1731 RSize -= J->size();
1732 }
1733 if (areJTsAllowed(TLI)) {
1734 // If our case is dense we *really* should handle it earlier!
1735 assert((FMetric > 0) && "Should handle dense range earlier!");
1736 } else {
1737 Pivot = CR.Range.first + Size/2;
1738 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001739
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740 CaseRange LHSR(CR.Range.first, Pivot);
1741 CaseRange RHSR(Pivot, CR.Range.second);
1742 Constant *C = Pivot->Low;
1743 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001744
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001746 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001747 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001748 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001749 // Pivot's Value, then we can branch directly to the LHS's Target,
1750 // rather than creating a leaf node for it.
1751 if ((LHSR.second - LHSR.first) == 1 &&
1752 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001753 cast<ConstantInt>(C)->getValue() ==
1754 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001755 TrueBB = LHSR.first->BB;
1756 } else {
1757 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1758 CurMF->insert(BBI, TrueBB);
1759 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001760
1761 // Put SV in a virtual register to make it available from the new blocks.
1762 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001763 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001764
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001765 // Similar to the optimization above, if the Value being switched on is
1766 // known to be less than the Constant CR.LT, and the current Case Value
1767 // is CR.LT - 1, then we can branch directly to the target block for
1768 // the current Case Value, rather than emitting a RHS leaf node for it.
1769 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001770 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1771 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001772 FalseBB = RHSR.first->BB;
1773 } else {
1774 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1775 CurMF->insert(BBI, FalseBB);
1776 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001777
1778 // Put SV in a virtual register to make it available from the new blocks.
1779 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001780 }
1781
1782 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001783 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001784 // Otherwise, branch to LHS.
1785 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1786
Dan Gohman99be8ae2010-04-19 22:41:47 +00001787 if (CR.CaseBB == SwitchBB)
1788 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001789 else
1790 SwitchCases.push_back(CB);
1791
1792 return true;
1793}
1794
1795/// handleBitTestsSwitchCase - if current case range has few destination and
1796/// range span less, than machine word bitwidth, encode case range into series
1797/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001798bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1799 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001800 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001801 MachineBasicBlock* Default,
1802 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001803 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001804 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001805
1806 Case& FrontCase = *CR.Range.first;
1807 Case& BackCase = *(CR.Range.second-1);
1808
1809 // Get the MachineFunction which holds the current MBB. This is used when
1810 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001811 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001813 // If target does not have legal shift left, do not emit bit tests at all.
1814 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1815 return false;
1816
Anton Korobeynikov23218582008-12-23 22:25:27 +00001817 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001818 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1819 I!=E; ++I) {
1820 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001822 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001823
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001824 // Count unique destinations
1825 SmallSet<MachineBasicBlock*, 4> Dests;
1826 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1827 Dests.insert(I->BB);
1828 if (Dests.size() > 3)
1829 // Don't bother the code below, if there are too much unique destinations
1830 return false;
1831 }
David Greene4b69d992010-01-05 01:24:57 +00001832 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001833 << Dests.size() << '\n'
1834 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001835
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001836 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001837 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1838 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001839 APInt cmpRange = maxValue - minValue;
1840
David Greene4b69d992010-01-05 01:24:57 +00001841 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001842 << "Low bound: " << minValue << '\n'
1843 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001844
Dan Gohmane0567812010-04-08 23:03:40 +00001845 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001846 (!(Dests.size() == 1 && numCmps >= 3) &&
1847 !(Dests.size() == 2 && numCmps >= 5) &&
1848 !(Dests.size() >= 3 && numCmps >= 6)))
1849 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001850
David Greene4b69d992010-01-05 01:24:57 +00001851 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001852 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1853
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001854 // Optimize the case where all the case values fit in a
1855 // word without having to subtract minValue. In this case,
1856 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001857 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001858 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001859 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001860 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001861 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001862
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 CaseBitsVector CasesBits;
1864 unsigned i, count = 0;
1865
1866 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1867 MachineBasicBlock* Dest = I->BB;
1868 for (i = 0; i < count; ++i)
1869 if (Dest == CasesBits[i].BB)
1870 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001871
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001872 if (i == count) {
1873 assert((count < 3) && "Too much destinations to test!");
1874 CasesBits.push_back(CaseBits(0, Dest, 0));
1875 count++;
1876 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001877
1878 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1879 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1880
1881 uint64_t lo = (lowValue - lowBound).getZExtValue();
1882 uint64_t hi = (highValue - lowBound).getZExtValue();
1883
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001884 for (uint64_t j = lo; j <= hi; j++) {
1885 CasesBits[i].Mask |= 1ULL << j;
1886 CasesBits[i].Bits++;
1887 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889 }
1890 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001891
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001892 BitTestInfo BTC;
1893
1894 // Figure out which block is immediately after the current one.
1895 MachineFunction::iterator BBI = CR.CaseBB;
1896 ++BBI;
1897
1898 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1899
David Greene4b69d992010-01-05 01:24:57 +00001900 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001901 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001902 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001903 << ", Bits: " << CasesBits[i].Bits
1904 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001905
1906 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1907 CurMF->insert(BBI, CaseBB);
1908 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1909 CaseBB,
1910 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001911
1912 // Put SV in a virtual register to make it available from the new blocks.
1913 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001915
1916 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001917 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918 CR.CaseBB, Default, BTC);
1919
Dan Gohman99be8ae2010-04-19 22:41:47 +00001920 if (CR.CaseBB == SwitchBB)
1921 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001922
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001923 BitTestCases.push_back(BTB);
1924
1925 return true;
1926}
1927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001928/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001929size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1930 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001931 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001932
1933 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001934 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001935 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1936 Cases.push_back(Case(SI.getSuccessorValue(i),
1937 SI.getSuccessorValue(i),
1938 SMBB));
1939 }
1940 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1941
1942 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001943 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001944 // Must recompute end() each iteration because it may be
1945 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001946 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1947 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1948 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001949 MachineBasicBlock* nextBB = J->BB;
1950 MachineBasicBlock* currentBB = I->BB;
1951
1952 // If the two neighboring cases go to the same destination, merge them
1953 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 I->High = J->High;
1956 J = Cases.erase(J);
1957 } else {
1958 I = J++;
1959 }
1960 }
1961
1962 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1963 if (I->Low != I->High)
1964 // A range counts double, since it requires two compares.
1965 ++numCmps;
1966 }
1967
1968 return numCmps;
1969}
1970
Dan Gohman46510a72010-04-15 01:51:59 +00001971void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001972 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1973
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001974 // Figure out which block is immediately after the current one.
1975 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1977
1978 // If there is only the default destination, branch to it if it is not the
1979 // next basic block. Otherwise, just fall through.
1980 if (SI.getNumOperands() == 2) {
1981 // Update machine-CFG edges.
1982
1983 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001984 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001985 if (Default != NextBlock)
1986 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1987 MVT::Other, getControlRoot(),
1988 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990 return;
1991 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001992
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001993 // If there are any non-default case statements, create a vector of Cases
1994 // representing each one, and sort the vector so that we can efficiently
1995 // create a binary search tree from them.
1996 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001997 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001998 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001999 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002000 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002001
2002 // Get the Value to be switched on and default basic blocks, which will be
2003 // inserted into CaseBlock records, representing basic blocks in the binary
2004 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002005 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002006
2007 // Push the initial CaseRec onto the worklist
2008 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002009 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2010 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002011
2012 while (!WorkList.empty()) {
2013 // Grab a record representing a case range to process off the worklist
2014 CaseRec CR = WorkList.back();
2015 WorkList.pop_back();
2016
Dan Gohman99be8ae2010-04-19 22:41:47 +00002017 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002018 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002019
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 // If the range has few cases (two or less) emit a series of specific
2021 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002022 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002025 // If the switch has more than 5 blocks, and at least 40% dense, and the
2026 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002027 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002028 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002029 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2032 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002033 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 }
2035}
2036
Dan Gohman46510a72010-04-15 01:51:59 +00002037void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002038 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2039
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002040 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002041 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002042 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002043 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002044 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002045 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002046 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2047 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002048 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002049
Bill Wendling4533cac2010-01-28 21:51:40 +00002050 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2051 MVT::Other, getControlRoot(),
2052 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002053}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002054
Dan Gohman46510a72010-04-15 01:51:59 +00002055void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056 // -0.0 - X --> fneg
2057 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002058 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2060 const VectorType *DestTy = cast<VectorType>(I.getType());
2061 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002062 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002063 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002064 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002065 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +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 Gohmanf0cbcd42008-09-03 16:12:24 +00002069 return;
2070 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002071 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002073
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002074 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002075 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002076 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002077 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2078 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002079 return;
2080 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002082 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083}
2084
Dan Gohman46510a72010-04-15 01:51:59 +00002085void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086 SDValue Op1 = getValue(I.getOperand(0));
2087 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002088 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2089 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090}
2091
Dan Gohman46510a72010-04-15 01:51:59 +00002092void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 SDValue Op1 = getValue(I.getOperand(0));
2094 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002095 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002096 Op2.getValueType() != TLI.getShiftAmountTy()) {
2097 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002098 EVT PTy = TLI.getPointerTy();
2099 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002100 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002101 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2102 TLI.getShiftAmountTy(), Op2);
2103 // If the operand is larger than the shift count type but the shift
2104 // count type has enough bits to represent any shift value, truncate
2105 // it now. This is a common case and it exposes the truncate to
2106 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002107 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002108 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2109 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2110 TLI.getShiftAmountTy(), Op2);
2111 // Otherwise we'll need to temporarily settle for some other
2112 // convenient type; type legalization will make adjustments as
2113 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002114 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002115 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002116 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002117 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002118 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002119 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002120 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002121
Bill Wendling4533cac2010-01-28 21:51:40 +00002122 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2123 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002124}
2125
Dan Gohman46510a72010-04-15 01:51:59 +00002126void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002128 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002130 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002131 predicate = ICmpInst::Predicate(IC->getPredicate());
2132 SDValue Op1 = getValue(I.getOperand(0));
2133 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002134 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002135
Owen Andersone50ed302009-08-10 22:56:29 +00002136 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002137 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138}
2139
Dan Gohman46510a72010-04-15 01:51:59 +00002140void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002142 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002144 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145 predicate = FCmpInst::Predicate(FC->getPredicate());
2146 SDValue Op1 = getValue(I.getOperand(0));
2147 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002148 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002149 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002150 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151}
2152
Dan Gohman46510a72010-04-15 01:51:59 +00002153void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002154 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002155 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2156 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002157 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002158
Bill Wendling49fcff82009-12-21 22:30:11 +00002159 SmallVector<SDValue, 4> Values(NumValues);
2160 SDValue Cond = getValue(I.getOperand(0));
2161 SDValue TrueVal = getValue(I.getOperand(1));
2162 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002163
Bill Wendling4533cac2010-01-28 21:51:40 +00002164 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002165 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002166 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2167 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002168 SDValue(TrueVal.getNode(),
2169 TrueVal.getResNo() + i),
2170 SDValue(FalseVal.getNode(),
2171 FalseVal.getResNo() + i));
2172
Bill Wendling4533cac2010-01-28 21:51:40 +00002173 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2174 DAG.getVTList(&ValueVTs[0], NumValues),
2175 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002176}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002177
Dan Gohman46510a72010-04-15 01:51:59 +00002178void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002179 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2180 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002181 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002182 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183}
2184
Dan Gohman46510a72010-04-15 01:51:59 +00002185void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2187 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2188 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002189 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002190 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191}
2192
Dan Gohman46510a72010-04-15 01:51:59 +00002193void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2195 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2196 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002197 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002198 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002199}
2200
Dan Gohman46510a72010-04-15 01:51:59 +00002201void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002202 // FPTrunc is never a no-op cast, no need to check
2203 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002204 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002205 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2206 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207}
2208
Dan Gohman46510a72010-04-15 01:51:59 +00002209void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210 // FPTrunc is never a no-op cast, no need to check
2211 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002212 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002213 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002214}
2215
Dan Gohman46510a72010-04-15 01:51:59 +00002216void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002217 // FPToUI is never a no-op cast, no need to check
2218 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002219 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002220 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221}
2222
Dan Gohman46510a72010-04-15 01:51:59 +00002223void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 // FPToSI is never a no-op cast, no need to check
2225 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002226 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002227 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228}
2229
Dan Gohman46510a72010-04-15 01:51:59 +00002230void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231 // UIToFP is never a no-op cast, no need to check
2232 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002233 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002234 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235}
2236
Dan Gohman46510a72010-04-15 01:51:59 +00002237void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002238 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002240 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002241 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002242}
2243
Dan Gohman46510a72010-04-15 01:51:59 +00002244void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002245 // What to do depends on the size of the integer and the size of the pointer.
2246 // We can either truncate, zero extend, or no-op, accordingly.
2247 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002248 EVT SrcVT = N.getValueType();
2249 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002250 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251}
2252
Dan Gohman46510a72010-04-15 01:51:59 +00002253void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002254 // What to do depends on the size of the integer and the size of the pointer.
2255 // We can either truncate, zero extend, or no-op, accordingly.
2256 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002257 EVT SrcVT = N.getValueType();
2258 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002259 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002260}
2261
Dan Gohman46510a72010-04-15 01:51:59 +00002262void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002263 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002264 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002265
Bill Wendling49fcff82009-12-21 22:30:11 +00002266 // BitCast assures us that source and destination are the same size so this is
2267 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002268 if (DestVT != N.getValueType())
2269 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2270 DestVT, N)); // convert types.
2271 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002272 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273}
2274
Dan Gohman46510a72010-04-15 01:51:59 +00002275void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002276 SDValue InVec = getValue(I.getOperand(0));
2277 SDValue InVal = getValue(I.getOperand(1));
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(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002281 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2282 TLI.getValueType(I.getType()),
2283 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002284}
2285
Dan Gohman46510a72010-04-15 01:51:59 +00002286void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002287 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002288 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002289 TLI.getPointerTy(),
2290 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002291 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2292 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002293}
2294
Mon P Wangaeb06d22008-11-10 04:46:22 +00002295// Utility for visitShuffleVector - Returns true if the mask is mask starting
2296// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002297static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2298 unsigned MaskNumElts = Mask.size();
2299 for (unsigned i = 0; i != MaskNumElts; ++i)
2300 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002301 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002302 return true;
2303}
2304
Dan Gohman46510a72010-04-15 01:51:59 +00002305void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002306 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002307 SDValue Src1 = getValue(I.getOperand(0));
2308 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309
Nate Begeman9008ca62009-04-27 18:41:29 +00002310 // Convert the ConstantVector mask operand into an array of ints, with -1
2311 // representing undef values.
2312 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002313 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002314 unsigned MaskNumElts = MaskElts.size();
2315 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002316 if (isa<UndefValue>(MaskElts[i]))
2317 Mask.push_back(-1);
2318 else
2319 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2320 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002321
Owen Andersone50ed302009-08-10 22:56:29 +00002322 EVT VT = TLI.getValueType(I.getType());
2323 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002324 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002325
Mon P Wangc7849c22008-11-16 05:06:27 +00002326 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002327 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2328 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002329 return;
2330 }
2331
2332 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002333 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2334 // Mask is longer than the source vectors and is a multiple of the source
2335 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002336 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002337 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2338 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002339 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2340 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002341 return;
2342 }
2343
Mon P Wangc7849c22008-11-16 05:06:27 +00002344 // Pad both vectors with undefs to make them the same length as the mask.
2345 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002346 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2347 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002348 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002349
Nate Begeman9008ca62009-04-27 18:41:29 +00002350 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2351 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002352 MOps1[0] = Src1;
2353 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002354
2355 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2356 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002357 &MOps1[0], NumConcat);
2358 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002359 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002360 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002361
Mon P Wangaeb06d22008-11-10 04:46:22 +00002362 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002363 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002364 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002365 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002366 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002367 MappedOps.push_back(Idx);
2368 else
2369 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002370 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002371
Bill Wendling4533cac2010-01-28 21:51:40 +00002372 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2373 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002374 return;
2375 }
2376
Mon P Wangc7849c22008-11-16 05:06:27 +00002377 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002378 // Analyze the access pattern of the vector to see if we can extract
2379 // two subvectors and do the shuffle. The analysis is done by calculating
2380 // the range of elements the mask access on both vectors.
2381 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2382 int MaxRange[2] = {-1, -1};
2383
Nate Begeman5a5ca152009-04-29 05:20:52 +00002384 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002385 int Idx = Mask[i];
2386 int Input = 0;
2387 if (Idx < 0)
2388 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002389
Nate Begeman5a5ca152009-04-29 05:20:52 +00002390 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002391 Input = 1;
2392 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002393 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002394 if (Idx > MaxRange[Input])
2395 MaxRange[Input] = Idx;
2396 if (Idx < MinRange[Input])
2397 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002398 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002399
Mon P Wangc7849c22008-11-16 05:06:27 +00002400 // Check if the access is smaller than the vector size and can we find
2401 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002402 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2403 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002404 int StartIdx[2]; // StartIdx to extract from
2405 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002406 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002407 RangeUse[Input] = 0; // Unused
2408 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002409 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002410 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002411 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002412 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002413 RangeUse[Input] = 1; // Extract from beginning of the vector
2414 StartIdx[Input] = 0;
2415 } else {
2416 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002417 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002418 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002419 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002420 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002421 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002422 }
2423
Bill Wendling636e2582009-08-21 18:16:06 +00002424 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002425 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002426 return;
2427 }
2428 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2429 // Extract appropriate subvector and generate a vector shuffle
2430 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002431 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002432 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002433 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002434 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002435 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002436 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002437 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002438
Mon P Wangc7849c22008-11-16 05:06:27 +00002439 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002440 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002441 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002442 int Idx = Mask[i];
2443 if (Idx < 0)
2444 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002445 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002446 MappedOps.push_back(Idx - StartIdx[0]);
2447 else
2448 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002449 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002450
Bill Wendling4533cac2010-01-28 21:51:40 +00002451 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2452 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002453 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002454 }
2455 }
2456
Mon P Wangc7849c22008-11-16 05:06:27 +00002457 // We can't use either concat vectors or extract subvectors so fall back to
2458 // replacing the shuffle with extract and build vector.
2459 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002460 EVT EltVT = VT.getVectorElementType();
2461 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002462 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002463 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002465 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002466 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002467 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002468 SDValue Res;
2469
Nate Begeman5a5ca152009-04-29 05:20:52 +00002470 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002471 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2472 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002473 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002474 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2475 EltVT, Src2,
2476 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2477
2478 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002479 }
2480 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002481
Bill Wendling4533cac2010-01-28 21:51:40 +00002482 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2483 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002484}
2485
Dan Gohman46510a72010-04-15 01:51:59 +00002486void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002487 const Value *Op0 = I.getOperand(0);
2488 const Value *Op1 = I.getOperand(1);
2489 const Type *AggTy = I.getType();
2490 const Type *ValTy = Op1->getType();
2491 bool IntoUndef = isa<UndefValue>(Op0);
2492 bool FromUndef = isa<UndefValue>(Op1);
2493
2494 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2495 I.idx_begin(), I.idx_end());
2496
Owen Andersone50ed302009-08-10 22:56:29 +00002497 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002498 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002499 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2501
2502 unsigned NumAggValues = AggValueVTs.size();
2503 unsigned NumValValues = ValValueVTs.size();
2504 SmallVector<SDValue, 4> Values(NumAggValues);
2505
2506 SDValue Agg = getValue(Op0);
2507 SDValue Val = getValue(Op1);
2508 unsigned i = 0;
2509 // Copy the beginning value(s) from the original aggregate.
2510 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002511 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002512 SDValue(Agg.getNode(), Agg.getResNo() + i);
2513 // Copy values from the inserted value(s).
2514 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002515 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002516 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2517 // Copy remaining value(s) from the original aggregate.
2518 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002519 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002520 SDValue(Agg.getNode(), Agg.getResNo() + i);
2521
Bill Wendling4533cac2010-01-28 21:51:40 +00002522 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2523 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2524 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002525}
2526
Dan Gohman46510a72010-04-15 01:51:59 +00002527void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002528 const Value *Op0 = I.getOperand(0);
2529 const Type *AggTy = Op0->getType();
2530 const Type *ValTy = I.getType();
2531 bool OutOfUndef = isa<UndefValue>(Op0);
2532
2533 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2534 I.idx_begin(), I.idx_end());
2535
Owen Andersone50ed302009-08-10 22:56:29 +00002536 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002537 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2538
2539 unsigned NumValValues = ValValueVTs.size();
2540 SmallVector<SDValue, 4> Values(NumValValues);
2541
2542 SDValue Agg = getValue(Op0);
2543 // Copy out the selected value(s).
2544 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2545 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002546 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002547 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002548 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002549
Bill Wendling4533cac2010-01-28 21:51:40 +00002550 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2551 DAG.getVTList(&ValValueVTs[0], NumValValues),
2552 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002553}
2554
Dan Gohman46510a72010-04-15 01:51:59 +00002555void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002556 SDValue N = getValue(I.getOperand(0));
2557 const Type *Ty = I.getOperand(0)->getType();
2558
Dan Gohman46510a72010-04-15 01:51:59 +00002559 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002560 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002561 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002562 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2563 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2564 if (Field) {
2565 // N = N + Offset
2566 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002567 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002568 DAG.getIntPtrConstant(Offset));
2569 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002570
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002571 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002572 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2573 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2574
2575 // Offset canonically 0 for unions, but type changes
2576 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002577 } else {
2578 Ty = cast<SequentialType>(Ty)->getElementType();
2579
2580 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002581 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002582 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002583 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002584 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002585 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002586 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002587 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002588 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002589 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2590 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002591 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002592 else
Evan Chengb1032a82009-02-09 20:54:38 +00002593 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002594
Dale Johannesen66978ee2009-01-31 02:22:37 +00002595 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002596 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002597 continue;
2598 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002599
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002600 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002601 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2602 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002603 SDValue IdxN = getValue(Idx);
2604
2605 // If the index is smaller or larger than intptr_t, truncate or extend
2606 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002607 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002608
2609 // If this is a multiply by a power of two, turn it into a shl
2610 // immediately. This is a very common case.
2611 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002612 if (ElementSize.isPowerOf2()) {
2613 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002614 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002615 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002616 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002617 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002618 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002619 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002620 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002621 }
2622 }
2623
Scott Michelfdc40a02009-02-17 22:15:04 +00002624 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002625 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002626 }
2627 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002628
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002629 setValue(&I, N);
2630}
2631
Dan Gohman46510a72010-04-15 01:51:59 +00002632void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002633 // If this is a fixed sized alloca in the entry block of the function,
2634 // allocate it statically on the stack.
2635 if (FuncInfo.StaticAllocaMap.count(&I))
2636 return; // getValue will auto-populate this.
2637
2638 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002639 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002640 unsigned Align =
2641 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2642 I.getAlignment());
2643
2644 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002645
Chris Lattner0b18e592009-03-17 19:36:00 +00002646 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2647 AllocSize,
2648 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002649
Owen Andersone50ed302009-08-10 22:56:29 +00002650 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002651 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002653 // Handle alignment. If the requested alignment is less than or equal to
2654 // the stack alignment, ignore it. If the size is greater than or equal to
2655 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002656 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002657 if (Align <= StackAlign)
2658 Align = 0;
2659
2660 // Round the size of the allocation up to the stack alignment size
2661 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002662 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002663 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002664 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002665
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002666 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002667 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002668 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002669 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2670
2671 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002672 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002673 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002674 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002675 setValue(&I, DSA);
2676 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002677
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002678 // Inform the Frame Information that we have just allocated a variable-sized
2679 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002680 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002681}
2682
Dan Gohman46510a72010-04-15 01:51:59 +00002683void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002684 const Value *SV = I.getOperand(0);
2685 SDValue Ptr = getValue(SV);
2686
2687 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002689 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002690 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691 unsigned Alignment = I.getAlignment();
2692
Owen Andersone50ed302009-08-10 22:56:29 +00002693 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 SmallVector<uint64_t, 4> Offsets;
2695 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2696 unsigned NumValues = ValueVTs.size();
2697 if (NumValues == 0)
2698 return;
2699
2700 SDValue Root;
2701 bool ConstantMemory = false;
2702 if (I.isVolatile())
2703 // Serialize volatile loads with other side effects.
2704 Root = getRoot();
2705 else if (AA->pointsToConstantMemory(SV)) {
2706 // Do not serialize (non-volatile) loads of constant memory with anything.
2707 Root = DAG.getEntryNode();
2708 ConstantMemory = true;
2709 } else {
2710 // Do not serialize non-volatile loads against each other.
2711 Root = DAG.getRoot();
2712 }
2713
2714 SmallVector<SDValue, 4> Values(NumValues);
2715 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002716 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002717 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002718 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2719 PtrVT, Ptr,
2720 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002721 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002722 A, SV, Offsets[i], isVolatile,
2723 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002724
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002725 Values[i] = L;
2726 Chains[i] = L.getValue(1);
2727 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002730 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002731 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732 if (isVolatile)
2733 DAG.setRoot(Chain);
2734 else
2735 PendingLoads.push_back(Chain);
2736 }
2737
Bill Wendling4533cac2010-01-28 21:51:40 +00002738 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2739 DAG.getVTList(&ValueVTs[0], NumValues),
2740 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002741}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002742
Dan Gohman46510a72010-04-15 01:51:59 +00002743void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2744 const Value *SrcV = I.getOperand(0);
2745 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002746
Owen Andersone50ed302009-08-10 22:56:29 +00002747 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748 SmallVector<uint64_t, 4> Offsets;
2749 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2750 unsigned NumValues = ValueVTs.size();
2751 if (NumValues == 0)
2752 return;
2753
2754 // Get the lowered operands. Note that we do this after
2755 // checking if NumResults is zero, because with zero results
2756 // the operands won't have values in the map.
2757 SDValue Src = getValue(SrcV);
2758 SDValue Ptr = getValue(PtrV);
2759
2760 SDValue Root = getRoot();
2761 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002762 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002763 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002764 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002766
2767 for (unsigned i = 0; i != NumValues; ++i) {
2768 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2769 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002770 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002771 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002772 Add, PtrV, Offsets[i], isVolatile,
2773 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002774 }
2775
Bill Wendling4533cac2010-01-28 21:51:40 +00002776 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2777 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778}
2779
2780/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2781/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002782void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002783 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784 bool HasChain = !I.doesNotAccessMemory();
2785 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2786
2787 // Build the operand list.
2788 SmallVector<SDValue, 8> Ops;
2789 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2790 if (OnlyLoad) {
2791 // We don't need to serialize loads against other loads.
2792 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002793 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002794 Ops.push_back(getRoot());
2795 }
2796 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002797
2798 // Info is set by getTgtMemInstrinsic
2799 TargetLowering::IntrinsicInfo Info;
2800 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2801
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002802 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002803 if (!IsTgtIntrinsic)
2804 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805
2806 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002807 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 SDValue Op = getValue(I.getOperand(i));
2809 assert(TLI.isTypeLegal(Op.getValueType()) &&
2810 "Intrinsic uses a non-legal type?");
2811 Ops.push_back(Op);
2812 }
2813
Owen Andersone50ed302009-08-10 22:56:29 +00002814 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002815 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2816#ifndef NDEBUG
2817 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2818 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2819 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002820 }
Bob Wilson8d919552009-07-31 22:41:21 +00002821#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002822
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002823 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002824 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825
Bob Wilson8d919552009-07-31 22:41:21 +00002826 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002827
2828 // Create the node.
2829 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002830 if (IsTgtIntrinsic) {
2831 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002832 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002833 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002834 Info.memVT, Info.ptrVal, Info.offset,
2835 Info.align, Info.vol,
2836 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002837 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002838 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002839 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002840 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002841 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002842 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002843 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002844 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002845 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002846 }
2847
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 if (HasChain) {
2849 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2850 if (OnlyLoad)
2851 PendingLoads.push_back(Chain);
2852 else
2853 DAG.setRoot(Chain);
2854 }
Bill Wendling856ff412009-12-22 00:12:37 +00002855
Benjamin Kramerf0127052010-01-05 13:12:22 +00002856 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002857 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002858 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002859 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002860 }
Bill Wendling856ff412009-12-22 00:12:37 +00002861
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002862 setValue(&I, Result);
2863 }
2864}
2865
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002866/// GetSignificand - Get the significand and build it into a floating-point
2867/// number with exponent of 1:
2868///
2869/// Op = (Op & 0x007fffff) | 0x3f800000;
2870///
2871/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002872static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002873GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002874 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2875 DAG.getConstant(0x007fffff, MVT::i32));
2876 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2877 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002878 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002879}
2880
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002881/// GetExponent - Get the exponent:
2882///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002883/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002884///
2885/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002886static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002887GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002888 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002889 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2890 DAG.getConstant(0x7f800000, MVT::i32));
2891 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002892 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002893 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2894 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002895 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002896}
2897
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002898/// getF32Constant - Get 32-bit floating point constant.
2899static SDValue
2900getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002901 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002902}
2903
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002904/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905/// visitIntrinsicCall: I is a call instruction
2906/// Op is the associated NodeType for I
2907const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002908SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2909 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002910 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002911 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002912 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002913 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002914 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002915 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002916 getValue(I.getOperand(2)),
2917 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 setValue(&I, L);
2919 DAG.setRoot(L.getValue(1));
2920 return 0;
2921}
2922
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002923// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002924const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002925SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002926 SDValue Op1 = getValue(I.getOperand(1));
2927 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002928
Owen Anderson825b72b2009-08-11 20:47:22 +00002929 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002930 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002931 return 0;
2932}
Bill Wendling74c37652008-12-09 22:08:41 +00002933
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002934/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2935/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002936void
Dan Gohman46510a72010-04-15 01:51:59 +00002937SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002938 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002939 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002940
Eric Christopher551754c2010-04-16 23:37:20 +00002941 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002942 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002943 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002944
2945 // Put the exponent in the right bit position for later addition to the
2946 // final result:
2947 //
2948 // #define LOG2OFe 1.4426950f
2949 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002950 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002951 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002953
2954 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002955 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2956 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002957
2958 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002959 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002960 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002961
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002962 if (LimitFloatPrecision <= 6) {
2963 // For floating-point precision of 6:
2964 //
2965 // TwoToFractionalPartOfX =
2966 // 0.997535578f +
2967 // (0.735607626f + 0.252464424f * x) * x;
2968 //
2969 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002970 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002971 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002972 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002973 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2975 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002976 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002978
2979 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002980 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002981 TwoToFracPartOfX, IntegerPartOfX);
2982
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002984 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2985 // For floating-point precision of 12:
2986 //
2987 // TwoToFractionalPartOfX =
2988 // 0.999892986f +
2989 // (0.696457318f +
2990 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2991 //
2992 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002993 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002994 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002996 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002997 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2998 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002999 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003000 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3001 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003002 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003004
3005 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003006 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003007 TwoToFracPartOfX, IntegerPartOfX);
3008
Owen Anderson825b72b2009-08-11 20:47:22 +00003009 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003010 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3011 // For floating-point precision of 18:
3012 //
3013 // TwoToFractionalPartOfX =
3014 // 0.999999982f +
3015 // (0.693148872f +
3016 // (0.240227044f +
3017 // (0.554906021e-1f +
3018 // (0.961591928e-2f +
3019 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3020 //
3021 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003022 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003023 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003025 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003026 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3027 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003028 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3030 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003031 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3033 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003034 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3036 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003037 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003038 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3039 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003040 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003041 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003043
3044 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003045 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003046 TwoToFracPartOfX, IntegerPartOfX);
3047
Owen Anderson825b72b2009-08-11 20:47:22 +00003048 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003049 }
3050 } else {
3051 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003052 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003053 getValue(I.getOperand(1)).getValueType(),
3054 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003055 }
3056
Dale Johannesen59e577f2008-09-05 18:38:42 +00003057 setValue(&I, result);
3058}
3059
Bill Wendling39150252008-09-09 20:39:27 +00003060/// visitLog - Lower a log intrinsic. Handles the special sequences for
3061/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003062void
Dan Gohman46510a72010-04-15 01:51:59 +00003063SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003064 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003065 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003066
Eric Christopher551754c2010-04-16 23:37:20 +00003067 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003068 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003069 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003070 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003071
3072 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003073 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003075 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003076
3077 // Get the significand and build it into a floating-point number with
3078 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003079 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003080
3081 if (LimitFloatPrecision <= 6) {
3082 // For floating-point precision of 6:
3083 //
3084 // LogofMantissa =
3085 // -1.1609546f +
3086 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003087 //
Bill Wendling39150252008-09-09 20:39:27 +00003088 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003090 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003092 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003093 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3094 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003095 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003096
Scott Michelfdc40a02009-02-17 22:15:04 +00003097 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003098 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003099 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3100 // For floating-point precision of 12:
3101 //
3102 // LogOfMantissa =
3103 // -1.7417939f +
3104 // (2.8212026f +
3105 // (-1.4699568f +
3106 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3107 //
3108 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003110 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003112 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003113 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3114 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003115 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3117 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003118 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3120 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003121 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003122
Scott Michelfdc40a02009-02-17 22:15:04 +00003123 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003125 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3126 // For floating-point precision of 18:
3127 //
3128 // LogOfMantissa =
3129 // -2.1072184f +
3130 // (4.2372794f +
3131 // (-3.7029485f +
3132 // (2.2781945f +
3133 // (-0.87823314f +
3134 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3135 //
3136 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003138 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003140 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003141 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3142 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003143 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3145 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003146 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3148 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003149 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3151 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003152 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3154 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003155 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003156
Scott Michelfdc40a02009-02-17 22:15:04 +00003157 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003159 }
3160 } else {
3161 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003162 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003163 getValue(I.getOperand(1)).getValueType(),
3164 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003165 }
3166
Dale Johannesen59e577f2008-09-05 18:38:42 +00003167 setValue(&I, result);
3168}
3169
Bill Wendling3eb59402008-09-09 00:28:24 +00003170/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3171/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003172void
Dan Gohman46510a72010-04-15 01:51:59 +00003173SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003174 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003175 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003176
Eric Christopher551754c2010-04-16 23:37:20 +00003177 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003178 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003179 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003180 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003181
Bill Wendling39150252008-09-09 20:39:27 +00003182 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003183 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003184
Bill Wendling3eb59402008-09-09 00:28:24 +00003185 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003186 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003187 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003188
Bill Wendling3eb59402008-09-09 00:28:24 +00003189 // Different possible minimax approximations of significand in
3190 // floating-point for various degrees of accuracy over [1,2].
3191 if (LimitFloatPrecision <= 6) {
3192 // For floating-point precision of 6:
3193 //
3194 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3195 //
3196 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003198 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003201 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3202 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003203 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003204
Scott Michelfdc40a02009-02-17 22:15:04 +00003205 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003207 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3208 // For floating-point precision of 12:
3209 //
3210 // Log2ofMantissa =
3211 // -2.51285454f +
3212 // (4.07009056f +
3213 // (-2.12067489f +
3214 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003215 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003216 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003218 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003220 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3222 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003223 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3225 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003226 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003227 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3228 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003229 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003230
Scott Michelfdc40a02009-02-17 22:15:04 +00003231 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003233 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3234 // For floating-point precision of 18:
3235 //
3236 // Log2ofMantissa =
3237 // -3.0400495f +
3238 // (6.1129976f +
3239 // (-5.3420409f +
3240 // (3.2865683f +
3241 // (-1.2669343f +
3242 // (0.27515199f -
3243 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3244 //
3245 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003247 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003249 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003250 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3251 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003252 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3254 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003255 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3257 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003258 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3260 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003261 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3263 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003264 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003265
Scott Michelfdc40a02009-02-17 22:15:04 +00003266 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003267 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003268 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003269 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003270 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003271 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003272 getValue(I.getOperand(1)).getValueType(),
3273 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003274 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003275
Dale Johannesen59e577f2008-09-05 18:38:42 +00003276 setValue(&I, result);
3277}
3278
Bill Wendling3eb59402008-09-09 00:28:24 +00003279/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3280/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003281void
Dan Gohman46510a72010-04-15 01:51:59 +00003282SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003283 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003284 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003285
Eric Christopher551754c2010-04-16 23:37:20 +00003286 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003287 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003288 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003290
Bill Wendling39150252008-09-09 20:39:27 +00003291 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003292 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003294 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003295
3296 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003297 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003298 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003299
3300 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003301 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003302 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003303 // Log10ofMantissa =
3304 // -0.50419619f +
3305 // (0.60948995f - 0.10380950f * x) * x;
3306 //
3307 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003308 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003309 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003311 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3313 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003314 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003315
Scott Michelfdc40a02009-02-17 22:15:04 +00003316 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003318 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3319 // For floating-point precision of 12:
3320 //
3321 // Log10ofMantissa =
3322 // -0.64831180f +
3323 // (0.91751397f +
3324 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3325 //
3326 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003328 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3332 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3335 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003336 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003337
Scott Michelfdc40a02009-02-17 22:15:04 +00003338 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003340 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003341 // For floating-point precision of 18:
3342 //
3343 // Log10ofMantissa =
3344 // -0.84299375f +
3345 // (1.5327582f +
3346 // (-1.0688956f +
3347 // (0.49102474f +
3348 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3349 //
3350 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003351 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003354 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3356 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003357 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3359 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003360 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3362 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003363 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3365 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003367
Scott Michelfdc40a02009-02-17 22:15:04 +00003368 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003370 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003371 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003372 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003373 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003374 getValue(I.getOperand(1)).getValueType(),
3375 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003376 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003377
Dale Johannesen59e577f2008-09-05 18:38:42 +00003378 setValue(&I, result);
3379}
3380
Bill Wendlinge10c8142008-09-09 22:39:21 +00003381/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3382/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003383void
Dan Gohman46510a72010-04-15 01:51:59 +00003384SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003385 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003386 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003387
Eric Christopher551754c2010-04-16 23:37:20 +00003388 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003389 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003390 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003391
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003393
3394 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3396 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003397
3398 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003400 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003401
3402 if (LimitFloatPrecision <= 6) {
3403 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003404 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003405 // TwoToFractionalPartOfX =
3406 // 0.997535578f +
3407 // (0.735607626f + 0.252464424f * x) * x;
3408 //
3409 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003410 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003411 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3415 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003416 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003418 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003420
Scott Michelfdc40a02009-02-17 22:15:04 +00003421 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003423 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3424 // For floating-point precision of 12:
3425 //
3426 // TwoToFractionalPartOfX =
3427 // 0.999892986f +
3428 // (0.696457318f +
3429 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3430 //
3431 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003433 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003435 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3437 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003438 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003439 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3440 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003441 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003442 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003443 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003445
Scott Michelfdc40a02009-02-17 22:15:04 +00003446 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003447 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003448 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3449 // For floating-point precision of 18:
3450 //
3451 // TwoToFractionalPartOfX =
3452 // 0.999999982f +
3453 // (0.693148872f +
3454 // (0.240227044f +
3455 // (0.554906021e-1f +
3456 // (0.961591928e-2f +
3457 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3458 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003462 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3464 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003465 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3467 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003468 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3470 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003471 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003472 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3473 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003474 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003475 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3476 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003477 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003478 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003479 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003480 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003481
Scott Michelfdc40a02009-02-17 22:15:04 +00003482 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003484 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003485 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003486 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003487 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003488 getValue(I.getOperand(1)).getValueType(),
3489 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003490 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003491
Dale Johannesen601d3c02008-09-05 01:48:15 +00003492 setValue(&I, result);
3493}
3494
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003495/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3496/// limited-precision mode with x == 10.0f.
3497void
Dan Gohman46510a72010-04-15 01:51:59 +00003498SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003499 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003500 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003501 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003502 bool IsExp10 = false;
3503
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003505 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003506 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3507 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3508 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3509 APFloat Ten(10.0f);
3510 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3511 }
3512 }
3513 }
3514
3515 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003516 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003517
3518 // Put the exponent in the right bit position for later addition to the
3519 // final result:
3520 //
3521 // #define LOG2OF10 3.3219281f
3522 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003524 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003526
3527 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003528 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3529 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003530
3531 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003533 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003534
3535 if (LimitFloatPrecision <= 6) {
3536 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003537 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003538 // twoToFractionalPartOfX =
3539 // 0.997535578f +
3540 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003541 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003542 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003544 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003546 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3548 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003549 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003551 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003553
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003554 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003556 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3557 // For floating-point precision of 12:
3558 //
3559 // TwoToFractionalPartOfX =
3560 // 0.999892986f +
3561 // (0.696457318f +
3562 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3563 //
3564 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3570 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003571 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3573 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003574 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003576 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003578
Scott Michelfdc40a02009-02-17 22:15:04 +00003579 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003581 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3582 // For floating-point precision of 18:
3583 //
3584 // TwoToFractionalPartOfX =
3585 // 0.999999982f +
3586 // (0.693148872f +
3587 // (0.240227044f +
3588 // (0.554906021e-1f +
3589 // (0.961591928e-2f +
3590 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3591 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003593 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003595 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3597 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003598 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3600 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003601 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3603 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003604 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003605 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3606 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003607 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003608 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3609 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003610 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003611 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003612 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003614
Scott Michelfdc40a02009-02-17 22:15:04 +00003615 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003617 }
3618 } else {
3619 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003620 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003621 getValue(I.getOperand(1)).getValueType(),
3622 getValue(I.getOperand(1)),
3623 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003624 }
3625
3626 setValue(&I, result);
3627}
3628
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003629
3630/// ExpandPowI - Expand a llvm.powi intrinsic.
3631static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3632 SelectionDAG &DAG) {
3633 // If RHS is a constant, we can expand this out to a multiplication tree,
3634 // otherwise we end up lowering to a call to __powidf2 (for example). When
3635 // optimizing for size, we only want to do this if the expansion would produce
3636 // a small number of multiplies, otherwise we do the full expansion.
3637 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3638 // Get the exponent as a positive value.
3639 unsigned Val = RHSC->getSExtValue();
3640 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003641
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003642 // powi(x, 0) -> 1.0
3643 if (Val == 0)
3644 return DAG.getConstantFP(1.0, LHS.getValueType());
3645
Dan Gohmanae541aa2010-04-15 04:33:49 +00003646 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003647 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3648 // If optimizing for size, don't insert too many multiplies. This
3649 // inserts up to 5 multiplies.
3650 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3651 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003652 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003653 // powi(x,15) generates one more multiply than it should), but this has
3654 // the benefit of being both really simple and much better than a libcall.
3655 SDValue Res; // Logically starts equal to 1.0
3656 SDValue CurSquare = LHS;
3657 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003658 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003659 if (Res.getNode())
3660 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3661 else
3662 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003663 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003664
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003665 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3666 CurSquare, CurSquare);
3667 Val >>= 1;
3668 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003669
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003670 // If the original was negative, invert the result, producing 1/(x*x*x).
3671 if (RHSC->getSExtValue() < 0)
3672 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3673 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3674 return Res;
3675 }
3676 }
3677
3678 // Otherwise, expand to a libcall.
3679 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3680}
3681
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003682/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
3683/// argument, create the corresponding DBG_VALUE machine instruction for it now.
3684/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003685bool
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003686SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI,
3687 const Value *V, MDNode *Variable,
Dan Gohman5d11ea32010-05-01 00:33:16 +00003688 uint64_t Offset,
3689 const SDValue &N) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003690 if (!isa<Argument>(V))
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003691 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003692
Devang Patel719f6a92010-04-29 20:40:36 +00003693 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela83ce982010-04-29 18:50:36 +00003694 // Ignore inlined function arguments here.
3695 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00003696 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00003697 return false;
3698
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003699 MachineBasicBlock *MBB = FuncInfo.MBBMap[DI.getParent()];
3700 if (MBB != &MF.front())
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003701 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003702
3703 unsigned Reg = 0;
3704 if (N.getOpcode() == ISD::CopyFromReg) {
3705 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Evan Cheng1deef272010-04-29 00:59:34 +00003706 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003707 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3708 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
3709 if (PR)
3710 Reg = PR;
3711 }
3712 }
3713
Evan Chenga36acad2010-04-29 06:33:38 +00003714 if (!Reg) {
3715 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
3716 if (VMI == FuncInfo.ValueMap.end())
3717 return false;
3718 Reg = VMI->second;
3719 }
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003720
3721 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
3722 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
3723 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00003724 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003725 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003726 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003727}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003728
Douglas Gregor7d9663c2010-05-11 06:17:44 +00003729// VisualStudio defines setjmp as _setjmp
3730#if defined(_MSC_VER) && defined(setjmp)
3731#define setjmp_undefined_for_visual_studio
3732#undef setjmp
3733#endif
3734
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003735/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3736/// we want to emit this as a call to a named external function, return the name
3737/// otherwise lower it and return null.
3738const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003739SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003740 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003741 SDValue Res;
3742
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003743 switch (Intrinsic) {
3744 default:
3745 // By default, turn this into a target intrinsic node.
3746 visitTargetIntrinsic(I, Intrinsic);
3747 return 0;
3748 case Intrinsic::vastart: visitVAStart(I); return 0;
3749 case Intrinsic::vaend: visitVAEnd(I); return 0;
3750 case Intrinsic::vacopy: visitVACopy(I); return 0;
3751 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003752 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003753 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003754 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003755 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003756 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003757 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003758 return 0;
3759 case Intrinsic::setjmp:
3760 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003761 case Intrinsic::longjmp:
3762 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003763 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003764 // Assert for address < 256 since we support only user defined address
3765 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003766 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003767 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003768 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003769 < 256 &&
3770 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003771 SDValue Op1 = getValue(I.getOperand(1));
3772 SDValue Op2 = getValue(I.getOperand(2));
3773 SDValue Op3 = getValue(I.getOperand(3));
3774 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3775 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003776 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003777 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003778 return 0;
3779 }
Chris Lattner824b9582008-11-21 16:42:48 +00003780 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003781 // Assert for address < 256 since we support only user defined address
3782 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003783 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003784 < 256 &&
3785 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003786 SDValue Op1 = getValue(I.getOperand(1));
3787 SDValue Op2 = getValue(I.getOperand(2));
3788 SDValue Op3 = getValue(I.getOperand(3));
3789 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3790 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003791 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003792 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003793 return 0;
3794 }
Chris Lattner824b9582008-11-21 16:42:48 +00003795 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003796 // Assert for address < 256 since we support only user defined address
3797 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003798 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003799 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003800 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003801 < 256 &&
3802 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003803 SDValue Op1 = getValue(I.getOperand(1));
3804 SDValue Op2 = getValue(I.getOperand(2));
3805 SDValue Op3 = getValue(I.getOperand(3));
3806 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3807 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003808
3809 // If the source and destination are known to not be aliases, we can
3810 // lower memmove as memcpy.
3811 uint64_t Size = -1ULL;
3812 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003813 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003814 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003815 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003816 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003817 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003818 return 0;
3819 }
3820
Mon P Wang20adc9d2010-04-04 03:10:48 +00003821 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003822 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003823 return 0;
3824 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003825 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00003826 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00003827 if (!DIVariable(DI.getVariable()).Verify())
Devang Patel7e1e31f2009-07-02 22:43:26 +00003828 return 0;
3829
Devang Patelac1ceb32009-10-09 22:42:28 +00003830 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003831 // Parameters are handled specially.
Devang Patelf38c6c82010-04-28 23:24:13 +00003832 bool isParameter =
3833 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
Dan Gohman46510a72010-04-15 01:51:59 +00003834 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003835 if (!Address)
3836 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003837 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003838 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003839 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003840 if (AI) {
3841 // Don't handle byval arguments or VLAs, for example.
3842 // Non-byval arguments are handled here (they refer to the stack temporary
3843 // alloca at this point).
3844 DenseMap<const AllocaInst*, int>::iterator SI =
3845 FuncInfo.StaticAllocaMap.find(AI);
3846 if (SI == FuncInfo.StaticAllocaMap.end())
3847 return 0; // VLAs.
3848 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003849
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003850 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3851 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3852 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
3853 }
3854
3855 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3856 // but do not always have a corresponding SDNode built. The SDNodeOrder
3857 // absolute, but not relative, values are different depending on whether
3858 // debug info exists.
3859 ++SDNodeOrder;
3860 SDValue &N = NodeMap[Address];
3861 SDDbgValue *SDV;
3862 if (N.getNode()) {
3863 if (isParameter && !AI) {
3864 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
3865 if (FINode)
3866 // Byval parameter. We have a frame index at this point.
3867 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
3868 0, dl, SDNodeOrder);
3869 else
3870 // Can't do anything with other non-AI cases yet. This might be a
3871 // parameter of a callee function that got inlined, for example.
3872 return 0;
3873 } else if (AI)
3874 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
3875 0, dl, SDNodeOrder);
3876 else
3877 // Can't do anything with other non-AI cases yet.
3878 return 0;
3879 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
3880 } else {
3881 // This isn't useful, but it shows what we're missing.
3882 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
3883 0, dl, SDNodeOrder);
3884 DAG.AddDbgValue(SDV, 0, isParameter);
3885 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003886 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003887 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003888 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003889 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00003890 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003891 return 0;
3892
3893 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003894 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003895 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003896 if (!V)
3897 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003898
3899 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3900 // but do not always have a corresponding SDNode built. The SDNodeOrder
3901 // absolute, but not relative, values are different depending on whether
3902 // debug info exists.
3903 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003904 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00003905 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003906 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
3907 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00003908 } else {
Devang Pateld47f3c82010-05-05 22:29:00 +00003909 bool createUndef = false;
3910 // FIXME : Why not use getValue() directly ?
Devang Patel00190342010-03-15 19:15:44 +00003911 SDValue &N = NodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003912 if (N.getNode()) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003913 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
3914 SDV = DAG.getDbgValue(Variable, N.getNode(),
3915 N.getResNo(), Offset, dl, SDNodeOrder);
3916 DAG.AddDbgValue(SDV, N.getNode(), false);
3917 }
Devang Pateld47f3c82010-05-05 22:29:00 +00003918 } else if (isa<PHINode>(V) && !V->use_empty()) {
3919 SDValue N = getValue(V);
3920 if (N.getNode()) {
3921 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
3922 SDV = DAG.getDbgValue(Variable, N.getNode(),
3923 N.getResNo(), Offset, dl, SDNodeOrder);
3924 DAG.AddDbgValue(SDV, N.getNode(), false);
3925 }
3926 } else
3927 createUndef = true;
3928 } else
3929 createUndef = true;
3930 if (createUndef) {
Devang Patel00190342010-03-15 19:15:44 +00003931 // We may expand this to cover more cases. One case where we have no
3932 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003933 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
3934 Offset, dl, SDNodeOrder);
3935 DAG.AddDbgValue(SDV, 0, false);
3936 }
Devang Patel00190342010-03-15 19:15:44 +00003937 }
3938
3939 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003940 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003941 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003942 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003943 // Don't handle byval struct arguments or VLAs, for example.
3944 if (!AI)
3945 return 0;
3946 DenseMap<const AllocaInst*, int>::iterator SI =
3947 FuncInfo.StaticAllocaMap.find(AI);
3948 if (SI == FuncInfo.StaticAllocaMap.end())
3949 return 0; // VLAs.
3950 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003951
Chris Lattner512063d2010-04-05 06:19:28 +00003952 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3953 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3954 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003955 return 0;
3956 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003957 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003958 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003959 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3960 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003962 SDValue Ops[1];
3963 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003964 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003965 setValue(&I, Op);
3966 DAG.setRoot(Op.getValue(1));
3967 return 0;
3968 }
3969
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003970 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003971 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003972 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003973 if (CallMBB->isLandingPad())
3974 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003975 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003976#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003977 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003978#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003979 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3980 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003981 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003982 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003983
Chris Lattner3a5815f2009-09-17 23:54:54 +00003984 // Insert the EHSELECTION instruction.
3985 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3986 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003987 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003988 Ops[1] = getRoot();
3989 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003990 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003991 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003992 return 0;
3993 }
3994
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003995 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003996 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003997 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003998 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3999 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004000 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004001 return 0;
4002 }
4003
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004004 case Intrinsic::eh_return_i32:
4005 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004006 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4007 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4008 MVT::Other,
4009 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00004010 getValue(I.getOperand(1)),
4011 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004012 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004013 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004014 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004015 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004016 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00004017 EVT VT = getValue(I.getOperand(1)).getValueType();
4018 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004019 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004020 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004021 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004022 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004023 TLI.getPointerTy()),
4024 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004025 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004026 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004027 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004028 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4029 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004030 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004031 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004032 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004033 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00004034 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00004035 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004036 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004037
Chris Lattner512063d2010-04-05 06:19:28 +00004038 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004039 return 0;
4040 }
4041
Mon P Wang77cdf302008-11-10 20:54:11 +00004042 case Intrinsic::convertff:
4043 case Intrinsic::convertfsi:
4044 case Intrinsic::convertfui:
4045 case Intrinsic::convertsif:
4046 case Intrinsic::convertuif:
4047 case Intrinsic::convertss:
4048 case Intrinsic::convertsu:
4049 case Intrinsic::convertus:
4050 case Intrinsic::convertuu: {
4051 ISD::CvtCode Code = ISD::CVT_INVALID;
4052 switch (Intrinsic) {
4053 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4054 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4055 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4056 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4057 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4058 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4059 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4060 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4061 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4062 }
Owen Andersone50ed302009-08-10 22:56:29 +00004063 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00004064 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004065 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4066 DAG.getValueType(DestVT),
4067 DAG.getValueType(getValue(Op1).getValueType()),
4068 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004069 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004070 Code);
4071 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004072 return 0;
4073 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004074 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004075 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004076 getValue(I.getOperand(1)).getValueType(),
4077 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004078 return 0;
4079 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00004080 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
4081 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004082 return 0;
4083 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004084 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004085 getValue(I.getOperand(1)).getValueType(),
4086 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004087 return 0;
4088 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004089 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004090 getValue(I.getOperand(1)).getValueType(),
4091 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004092 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004093 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004094 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004095 return 0;
4096 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004097 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004098 return 0;
4099 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004100 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004101 return 0;
4102 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004103 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004104 return 0;
4105 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004106 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004107 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004108 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004109 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004110 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004111 case Intrinsic::convert_to_fp16:
4112 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004113 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004114 return 0;
4115 case Intrinsic::convert_from_fp16:
4116 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004117 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004118 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004119 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004120 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004121 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004122 return 0;
4123 }
4124 case Intrinsic::readcyclecounter: {
4125 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004126 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4127 DAG.getVTList(MVT::i64, MVT::Other),
4128 &Op, 1);
4129 setValue(&I, Res);
4130 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131 return 0;
4132 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004133 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004134 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004135 getValue(I.getOperand(1)).getValueType(),
4136 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 return 0;
4138 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004139 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004140 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004141 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004142 return 0;
4143 }
4144 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004145 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004146 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004147 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004148 return 0;
4149 }
4150 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004151 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004152 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004153 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004154 return 0;
4155 }
4156 case Intrinsic::stacksave: {
4157 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004158 Res = DAG.getNode(ISD::STACKSAVE, dl,
4159 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4160 setValue(&I, Res);
4161 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004162 return 0;
4163 }
4164 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004165 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004166 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004167 return 0;
4168 }
Bill Wendling57344502008-11-18 11:01:33 +00004169 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004170 // Emit code into the DAG to store the stack guard onto the stack.
4171 MachineFunction &MF = DAG.getMachineFunction();
4172 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004173 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004174
Eric Christopher551754c2010-04-16 23:37:20 +00004175 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4176 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004177
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004178 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004179 MFI->setStackProtectorIndex(FI);
4180
4181 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4182
4183 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004184 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4185 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004186 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004187 setValue(&I, Res);
4188 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004189 return 0;
4190 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004191 case Intrinsic::objectsize: {
4192 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004193 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004194
4195 assert(CI && "Non-constant type in __builtin_object_size?");
4196
Eric Christopher551754c2010-04-16 23:37:20 +00004197 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004198 EVT Ty = Arg.getValueType();
4199
Eric Christopherd060b252009-12-23 02:51:48 +00004200 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004201 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004202 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004203 Res = DAG.getConstant(0, Ty);
4204
4205 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004206 return 0;
4207 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004208 case Intrinsic::var_annotation:
4209 // Discard annotate attributes
4210 return 0;
4211
4212 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004213 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004214
4215 SDValue Ops[6];
4216 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004217 Ops[1] = getValue(I.getOperand(1));
4218 Ops[2] = getValue(I.getOperand(2));
4219 Ops[3] = getValue(I.getOperand(3));
4220 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004221 Ops[5] = DAG.getSrcValue(F);
4222
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004223 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4224 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4225 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004226
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004227 setValue(&I, Res);
4228 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004229 return 0;
4230 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004231 case Intrinsic::gcroot:
4232 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004233 const Value *Alloca = I.getOperand(1);
4234 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004235
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004236 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4237 GFI->addStackRoot(FI->getIndex(), TypeMap);
4238 }
4239 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004240 case Intrinsic::gcread:
4241 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004242 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004243 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004244 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004245 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004246 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004247 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004248 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004249 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004250 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004251 return implVisitAluOverflow(I, ISD::UADDO);
4252 case Intrinsic::sadd_with_overflow:
4253 return implVisitAluOverflow(I, ISD::SADDO);
4254 case Intrinsic::usub_with_overflow:
4255 return implVisitAluOverflow(I, ISD::USUBO);
4256 case Intrinsic::ssub_with_overflow:
4257 return implVisitAluOverflow(I, ISD::SSUBO);
4258 case Intrinsic::umul_with_overflow:
4259 return implVisitAluOverflow(I, ISD::UMULO);
4260 case Intrinsic::smul_with_overflow:
4261 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004263 case Intrinsic::prefetch: {
4264 SDValue Ops[4];
4265 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004266 Ops[1] = getValue(I.getOperand(1));
4267 Ops[2] = getValue(I.getOperand(2));
4268 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004269 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004270 return 0;
4271 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004273 case Intrinsic::memory_barrier: {
4274 SDValue Ops[6];
4275 Ops[0] = getRoot();
4276 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004277 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004278
Bill Wendling4533cac2010-01-28 21:51:40 +00004279 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004280 return 0;
4281 }
4282 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004283 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004284 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004285 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004286 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004287 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004288 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004289 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004290 getValue(I.getOperand(3)),
4291 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004292 setValue(&I, L);
4293 DAG.setRoot(L.getValue(1));
4294 return 0;
4295 }
4296 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004297 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004298 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004299 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004300 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004301 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004302 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004303 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004304 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004305 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004306 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004307 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004309 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004310 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004311 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004312 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004313 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004314 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004315 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004316 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004317 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004318
4319 case Intrinsic::invariant_start:
4320 case Intrinsic::lifetime_start:
4321 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004322 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004323 return 0;
4324 case Intrinsic::invariant_end:
4325 case Intrinsic::lifetime_end:
4326 // Discard region information.
4327 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004328 }
4329}
4330
Dan Gohman46510a72010-04-15 01:51:59 +00004331void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004332 bool isTailCall,
4333 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004334 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4335 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004336 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004337 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004338 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004339
4340 TargetLowering::ArgListTy Args;
4341 TargetLowering::ArgListEntry Entry;
4342 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004343
4344 // Check whether the function can return without sret-demotion.
4345 SmallVector<EVT, 4> OutVTs;
4346 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4347 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004348 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004349 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004350
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004351 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004352 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4353
4354 SDValue DemoteStackSlot;
4355
4356 if (!CanLowerReturn) {
4357 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4358 FTy->getReturnType());
4359 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4360 FTy->getReturnType());
4361 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004362 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004363 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4364
4365 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4366 Entry.Node = DemoteStackSlot;
4367 Entry.Ty = StackSlotPtrType;
4368 Entry.isSExt = false;
4369 Entry.isZExt = false;
4370 Entry.isInReg = false;
4371 Entry.isSRet = true;
4372 Entry.isNest = false;
4373 Entry.isByVal = false;
4374 Entry.Alignment = Align;
4375 Args.push_back(Entry);
4376 RetTy = Type::getVoidTy(FTy->getContext());
4377 }
4378
Dan Gohman46510a72010-04-15 01:51:59 +00004379 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004380 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004381 SDValue ArgNode = getValue(*i);
4382 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4383
4384 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004385 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4386 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4387 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4388 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4389 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4390 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391 Entry.Alignment = CS.getParamAlignment(attrInd);
4392 Args.push_back(Entry);
4393 }
4394
Chris Lattner512063d2010-04-05 06:19:28 +00004395 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004396 // Insert a label before the invoke call to mark the try range. This can be
4397 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004398 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004399
Jim Grosbachca752c92010-01-28 01:45:32 +00004400 // For SjLj, keep track of which landing pads go with which invokes
4401 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004402 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004403 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004404 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004405 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004406 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004407 }
4408
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004409 // Both PendingLoads and PendingExports must be flushed here;
4410 // this call might not return.
4411 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004412 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413 }
4414
Dan Gohman98ca4f22009-08-05 01:29:28 +00004415 // Check if target-independent constraints permit a tail call here.
4416 // Target-dependent constraints are checked within TLI.LowerCallTo.
4417 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004418 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004419 isTailCall = false;
4420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004421 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004422 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004423 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004424 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004425 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004426 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004427 isTailCall,
4428 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004429 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004430 assert((isTailCall || Result.second.getNode()) &&
4431 "Non-null chain expected with non-tail call!");
4432 assert((Result.second.getNode() || !Result.first.getNode()) &&
4433 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004434 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004435 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004436 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004437 // The instruction result is the result of loading from the
4438 // hidden sret parameter.
4439 SmallVector<EVT, 1> PVTs;
4440 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4441
4442 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4443 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4444 EVT PtrVT = PVTs[0];
4445 unsigned NumValues = OutVTs.size();
4446 SmallVector<SDValue, 4> Values(NumValues);
4447 SmallVector<SDValue, 4> Chains(NumValues);
4448
4449 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004450 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4451 DemoteStackSlot,
4452 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004453 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004454 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004455 Values[i] = L;
4456 Chains[i] = L.getValue(1);
4457 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004458
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004459 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4460 MVT::Other, &Chains[0], NumValues);
4461 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004462
4463 // Collect the legal value parts into potentially illegal values
4464 // that correspond to the original function's return values.
4465 SmallVector<EVT, 4> RetTys;
4466 RetTy = FTy->getReturnType();
4467 ComputeValueVTs(TLI, RetTy, RetTys);
4468 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4469 SmallVector<SDValue, 4> ReturnValues;
4470 unsigned CurReg = 0;
4471 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4472 EVT VT = RetTys[I];
4473 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4474 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4475
4476 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004477 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004478 RegisterVT, VT, AssertOp);
4479 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004480 CurReg += NumRegs;
4481 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004482
Bill Wendling4533cac2010-01-28 21:51:40 +00004483 setValue(CS.getInstruction(),
4484 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4485 DAG.getVTList(&RetTys[0], RetTys.size()),
4486 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004487
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004488 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004489
4490 // As a special case, a null chain means that a tail call has been emitted and
4491 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004492 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004493 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004494 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004495 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004496
Chris Lattner512063d2010-04-05 06:19:28 +00004497 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004498 // Insert a label at the end of the invoke call to mark the try range. This
4499 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004500 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004501 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004502
4503 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004504 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004505 }
4506}
4507
Chris Lattner8047d9a2009-12-24 00:37:38 +00004508/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4509/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004510static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4511 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004512 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004513 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004514 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004515 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004516 if (C->isNullValue())
4517 continue;
4518 // Unknown instruction.
4519 return false;
4520 }
4521 return true;
4522}
4523
Dan Gohman46510a72010-04-15 01:51:59 +00004524static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4525 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004526 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004527
Chris Lattner8047d9a2009-12-24 00:37:38 +00004528 // Check to see if this load can be trivially constant folded, e.g. if the
4529 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004530 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004531 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004532 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004533 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004534
Dan Gohman46510a72010-04-15 01:51:59 +00004535 if (const Constant *LoadCst =
4536 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4537 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004538 return Builder.getValue(LoadCst);
4539 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004540
Chris Lattner8047d9a2009-12-24 00:37:38 +00004541 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4542 // still constant memory, the input chain can be the entry node.
4543 SDValue Root;
4544 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004545
Chris Lattner8047d9a2009-12-24 00:37:38 +00004546 // Do not serialize (non-volatile) loads of constant memory with anything.
4547 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4548 Root = Builder.DAG.getEntryNode();
4549 ConstantMemory = true;
4550 } else {
4551 // Do not serialize non-volatile loads against each other.
4552 Root = Builder.DAG.getRoot();
4553 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004554
Chris Lattner8047d9a2009-12-24 00:37:38 +00004555 SDValue Ptr = Builder.getValue(PtrVal);
4556 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4557 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004558 false /*volatile*/,
4559 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004560
Chris Lattner8047d9a2009-12-24 00:37:38 +00004561 if (!ConstantMemory)
4562 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4563 return LoadVal;
4564}
4565
4566
4567/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4568/// If so, return true and lower it, otherwise return false and it will be
4569/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004570bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004571 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4572 if (I.getNumOperands() != 4)
4573 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004574
Eric Christopher551754c2010-04-16 23:37:20 +00004575 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004576 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004577 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004578 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004579 return false;
4580
Eric Christopher551754c2010-04-16 23:37:20 +00004581 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004582
Chris Lattner8047d9a2009-12-24 00:37:38 +00004583 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4584 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004585 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4586 bool ActuallyDoIt = true;
4587 MVT LoadVT;
4588 const Type *LoadTy;
4589 switch (Size->getZExtValue()) {
4590 default:
4591 LoadVT = MVT::Other;
4592 LoadTy = 0;
4593 ActuallyDoIt = false;
4594 break;
4595 case 2:
4596 LoadVT = MVT::i16;
4597 LoadTy = Type::getInt16Ty(Size->getContext());
4598 break;
4599 case 4:
4600 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004601 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004602 break;
4603 case 8:
4604 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004605 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004606 break;
4607 /*
4608 case 16:
4609 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004610 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004611 LoadTy = VectorType::get(LoadTy, 4);
4612 break;
4613 */
4614 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004615
Chris Lattner04b091a2009-12-24 01:07:17 +00004616 // This turns into unaligned loads. We only do this if the target natively
4617 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4618 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004619
Chris Lattner04b091a2009-12-24 01:07:17 +00004620 // Require that we can find a legal MVT, and only do this if the target
4621 // supports unaligned loads of that type. Expanding into byte loads would
4622 // bloat the code.
4623 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4624 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4625 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4626 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4627 ActuallyDoIt = false;
4628 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004629
Chris Lattner04b091a2009-12-24 01:07:17 +00004630 if (ActuallyDoIt) {
4631 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4632 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004633
Chris Lattner04b091a2009-12-24 01:07:17 +00004634 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4635 ISD::SETNE);
4636 EVT CallVT = TLI.getValueType(I.getType(), true);
4637 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4638 return true;
4639 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004640 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004641
4642
Chris Lattner8047d9a2009-12-24 00:37:38 +00004643 return false;
4644}
4645
4646
Dan Gohman46510a72010-04-15 01:51:59 +00004647void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648 const char *RenameFn = 0;
4649 if (Function *F = I.getCalledFunction()) {
4650 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004651 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004652 if (II) {
4653 if (unsigned IID = II->getIntrinsicID(F)) {
4654 RenameFn = visitIntrinsicCall(I, IID);
4655 if (!RenameFn)
4656 return;
4657 }
4658 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004659 if (unsigned IID = F->getIntrinsicID()) {
4660 RenameFn = visitIntrinsicCall(I, IID);
4661 if (!RenameFn)
4662 return;
4663 }
4664 }
4665
4666 // Check for well-known libc/libm calls. If the function is internal, it
4667 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004668 if (!F->hasLocalLinkage() && F->hasName()) {
4669 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004670 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004672 I.getOperand(1)->getType()->isFloatingPointTy() &&
4673 I.getType() == I.getOperand(1)->getType() &&
4674 I.getType() == I.getOperand(2)->getType()) {
4675 SDValue LHS = getValue(I.getOperand(1));
4676 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004677 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4678 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004679 return;
4680 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004681 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004682 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004683 I.getOperand(1)->getType()->isFloatingPointTy() &&
4684 I.getType() == I.getOperand(1)->getType()) {
4685 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004686 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4687 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688 return;
4689 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004690 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004692 I.getOperand(1)->getType()->isFloatingPointTy() &&
4693 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004694 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004695 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004696 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4697 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004698 return;
4699 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004700 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004701 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004702 I.getOperand(1)->getType()->isFloatingPointTy() &&
4703 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004704 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004705 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004706 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4707 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004708 return;
4709 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004710 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4711 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004712 I.getOperand(1)->getType()->isFloatingPointTy() &&
4713 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004714 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004715 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004716 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4717 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004718 return;
4719 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004720 } else if (Name == "memcmp") {
4721 if (visitMemCmpCall(I))
4722 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004723 }
4724 }
Eric Christopher551754c2010-04-16 23:37:20 +00004725 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004726 visitInlineAsm(&I);
4727 return;
4728 }
4729
4730 SDValue Callee;
4731 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004732 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004733 else
Bill Wendling056292f2008-09-16 21:48:12 +00004734 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735
Bill Wendling0d580132009-12-23 01:28:19 +00004736 // Check if we can potentially perform a tail call. More detailed checking is
4737 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004738 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739}
4740
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004741/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004742/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004743/// Chain/Flag as the input and updates them for the output Chain/Flag.
4744/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004745SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004746 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004747 // Assemble the legal parts into the final values.
4748 SmallVector<SDValue, 4> Values(ValueVTs.size());
4749 SmallVector<SDValue, 8> Parts;
4750 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4751 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004752 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004753 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004754 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004755
4756 Parts.resize(NumRegs);
4757 for (unsigned i = 0; i != NumRegs; ++i) {
4758 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004759 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004760 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004761 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004762 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004763 *Flag = P.getValue(2);
4764 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004767
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 // If the source register was virtual and if we know something about it,
4769 // add an assert node.
4770 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4771 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4772 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4773 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4774 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4775 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004776
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004777 unsigned RegSize = RegisterVT.getSizeInBits();
4778 unsigned NumSignBits = LOI.NumSignBits;
4779 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004780
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004781 // FIXME: We capture more information than the dag can represent. For
4782 // now, just use the tightest assertzext/assertsext possible.
4783 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004785 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004786 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004787 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004788 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004789 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004791 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004792 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004795 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004796 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004799 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004801
Bill Wendling4533cac2010-01-28 21:51:40 +00004802 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004803 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004804 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 }
4806 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004808 Parts[i] = P;
4809 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004810
Bill Wendling46ada192010-03-02 01:55:18 +00004811 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004812 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813 Part += NumRegs;
4814 Parts.clear();
4815 }
4816
Bill Wendling4533cac2010-01-28 21:51:40 +00004817 return DAG.getNode(ISD::MERGE_VALUES, dl,
4818 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4819 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004820}
4821
4822/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004823/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004824/// Chain/Flag as the input and updates them for the output Chain/Flag.
4825/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004826void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004827 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 // Get the list of the values's legal parts.
4829 unsigned NumRegs = Regs.size();
4830 SmallVector<SDValue, 8> Parts(NumRegs);
4831 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004832 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004833 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004834 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004835
Bill Wendling46ada192010-03-02 01:55:18 +00004836 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004837 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 &Parts[Part], NumParts, RegisterVT);
4839 Part += NumParts;
4840 }
4841
4842 // Copy the parts into the registers.
4843 SmallVector<SDValue, 8> Chains(NumRegs);
4844 for (unsigned i = 0; i != NumRegs; ++i) {
4845 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004846 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004847 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004848 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004849 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850 *Flag = Part.getValue(1);
4851 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004853 Chains[i] = Part.getValue(0);
4854 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004855
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004856 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004857 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004858 // flagged to it. That is the CopyToReg nodes and the user are considered
4859 // a single scheduling unit. If we create a TokenFactor and return it as
4860 // chain, then the TokenFactor is both a predecessor (operand) of the
4861 // user as well as a successor (the TF operands are flagged to the user).
4862 // c1, f1 = CopyToReg
4863 // c2, f2 = CopyToReg
4864 // c3 = TokenFactor c1, c2
4865 // ...
4866 // = op c3, ..., f2
4867 Chain = Chains[NumRegs-1];
4868 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870}
4871
4872/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004873/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004874/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004875void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4876 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004877 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004878 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004879 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004880 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004881 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004882 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004883 Ops.push_back(Res);
4884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004885 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004886 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004887 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004888 for (unsigned i = 0; i != NumRegs; ++i) {
4889 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004890 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004891 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004892 }
4893}
4894
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004895/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004896/// i.e. it isn't a stack pointer or some other special register, return the
4897/// register class for the register. Otherwise, return null.
4898static const TargetRegisterClass *
4899isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4900 const TargetLowering &TLI,
4901 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004902 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004903 const TargetRegisterClass *FoundRC = 0;
4904 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4905 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004906 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004907
4908 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004909 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004910 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4911 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4912 I != E; ++I) {
4913 if (TLI.isTypeLegal(*I)) {
4914 // If we have already found this register in a different register class,
4915 // choose the one with the largest VT specified. For example, on
4916 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004918 ThisVT = *I;
4919 break;
4920 }
4921 }
4922 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004923
Owen Anderson825b72b2009-08-11 20:47:22 +00004924 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004925
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004926 // NOTE: This isn't ideal. In particular, this might allocate the
4927 // frame pointer in functions that need it (due to them not being taken
4928 // out of allocation, because a variable sized allocation hasn't been seen
4929 // yet). This is a slight code pessimization, but should still work.
4930 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4931 E = RC->allocation_order_end(MF); I != E; ++I)
4932 if (*I == Reg) {
4933 // We found a matching register class. Keep looking at others in case
4934 // we find one with larger registers that this physreg is also in.
4935 FoundRC = RC;
4936 FoundVT = ThisVT;
4937 break;
4938 }
4939 }
4940 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004941}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004942
4943
4944namespace llvm {
4945/// AsmOperandInfo - This contains information for each constraint that we are
4946/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004947class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004948 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004949public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004950 /// CallOperand - If this is the result output operand or a clobber
4951 /// this is null, otherwise it is the incoming operand to the CallInst.
4952 /// This gets modified as the asm is processed.
4953 SDValue CallOperand;
4954
4955 /// AssignedRegs - If this is a register or register class operand, this
4956 /// contains the set of register corresponding to the operand.
4957 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004959 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4960 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4961 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004962
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004963 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4964 /// busy in OutputRegs/InputRegs.
4965 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004966 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004967 std::set<unsigned> &InputRegs,
4968 const TargetRegisterInfo &TRI) const {
4969 if (isOutReg) {
4970 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4971 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4972 }
4973 if (isInReg) {
4974 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4975 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4976 }
4977 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004978
Owen Andersone50ed302009-08-10 22:56:29 +00004979 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004980 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004982 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004983 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004984 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004986
Chris Lattner81249c92008-10-17 17:05:25 +00004987 if (isa<BasicBlock>(CallOperandVal))
4988 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004989
Chris Lattner81249c92008-10-17 17:05:25 +00004990 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004991
Chris Lattner81249c92008-10-17 17:05:25 +00004992 // If this is an indirect operand, the operand is a pointer to the
4993 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004994 if (isIndirect) {
4995 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4996 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004997 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004998 OpTy = PtrTy->getElementType();
4999 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005000
Chris Lattner81249c92008-10-17 17:05:25 +00005001 // If OpTy is not a single value, it may be a struct/union that we
5002 // can tile with integers.
5003 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5004 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5005 switch (BitSize) {
5006 default: break;
5007 case 1:
5008 case 8:
5009 case 16:
5010 case 32:
5011 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005012 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005013 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005014 break;
5015 }
5016 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005017
Chris Lattner81249c92008-10-17 17:05:25 +00005018 return TLI.getValueType(OpTy, true);
5019 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005021private:
5022 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5023 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005024 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005025 const TargetRegisterInfo &TRI) {
5026 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5027 Regs.insert(Reg);
5028 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5029 for (; *Aliases; ++Aliases)
5030 Regs.insert(*Aliases);
5031 }
5032};
5033} // end llvm namespace.
5034
5035
5036/// GetRegistersForValue - Assign registers (virtual or physical) for the
5037/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005038/// register allocator to handle the assignment process. However, if the asm
5039/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005040/// allocation. This produces generally horrible, but correct, code.
5041///
5042/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005043/// Input and OutputRegs are the set of already allocated physical registers.
5044///
Dan Gohman2048b852009-11-23 18:04:58 +00005045void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005046GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005047 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005048 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005049 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005051 // Compute whether this value requires an input register, an output register,
5052 // or both.
5053 bool isOutReg = false;
5054 bool isInReg = false;
5055 switch (OpInfo.Type) {
5056 case InlineAsm::isOutput:
5057 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005058
5059 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005060 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005061 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005062 break;
5063 case InlineAsm::isInput:
5064 isInReg = true;
5065 isOutReg = false;
5066 break;
5067 case InlineAsm::isClobber:
5068 isOutReg = true;
5069 isInReg = true;
5070 break;
5071 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005072
5073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005074 MachineFunction &MF = DAG.getMachineFunction();
5075 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005076
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005077 // If this is a constraint for a single physreg, or a constraint for a
5078 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005079 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005080 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5081 OpInfo.ConstraintVT);
5082
5083 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005084 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005085 // If this is a FP input in an integer register (or visa versa) insert a bit
5086 // cast of the input value. More generally, handle any case where the input
5087 // value disagrees with the register class we plan to stick this in.
5088 if (OpInfo.Type == InlineAsm::isInput &&
5089 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005090 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005091 // types are identical size, use a bitcast to convert (e.g. two differing
5092 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005093 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005094 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005095 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005096 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005097 OpInfo.ConstraintVT = RegVT;
5098 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5099 // If the input is a FP value and we want it in FP registers, do a
5100 // bitcast to the corresponding integer type. This turns an f64 value
5101 // into i64, which can be passed with two i32 values on a 32-bit
5102 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005103 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005104 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005105 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005106 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005107 OpInfo.ConstraintVT = RegVT;
5108 }
5109 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005110
Owen Anderson23b9b192009-08-12 00:36:31 +00005111 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005112 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005113
Owen Andersone50ed302009-08-10 22:56:29 +00005114 EVT RegVT;
5115 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005116
5117 // If this is a constraint for a specific physical register, like {r17},
5118 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005119 if (unsigned AssignedReg = PhysReg.first) {
5120 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005122 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005124 // Get the actual register value type. This is important, because the user
5125 // may have asked for (e.g.) the AX register in i32 type. We need to
5126 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005127 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005129 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005130 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005131
5132 // If this is an expanded reference, add the rest of the regs to Regs.
5133 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005134 TargetRegisterClass::iterator I = RC->begin();
5135 for (; *I != AssignedReg; ++I)
5136 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005138 // Already added the first reg.
5139 --NumRegs; ++I;
5140 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005141 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005142 Regs.push_back(*I);
5143 }
5144 }
Bill Wendling651ad132009-12-22 01:25:10 +00005145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005146 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5147 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5148 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5149 return;
5150 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005151
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005152 // Otherwise, if this was a reference to an LLVM register class, create vregs
5153 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005154 if (const TargetRegisterClass *RC = PhysReg.second) {
5155 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005156 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005157 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005158
Evan Chengfb112882009-03-23 08:01:15 +00005159 // Create the appropriate number of virtual registers.
5160 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5161 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005162 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005163
Evan Chengfb112882009-03-23 08:01:15 +00005164 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5165 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005166 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005167
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005168 // This is a reference to a register class that doesn't directly correspond
5169 // to an LLVM register class. Allocate NumRegs consecutive, available,
5170 // registers from the class.
5171 std::vector<unsigned> RegClassRegs
5172 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5173 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005174
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005175 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5176 unsigned NumAllocated = 0;
5177 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5178 unsigned Reg = RegClassRegs[i];
5179 // See if this register is available.
5180 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5181 (isInReg && InputRegs.count(Reg))) { // Already used.
5182 // Make sure we find consecutive registers.
5183 NumAllocated = 0;
5184 continue;
5185 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005187 // Check to see if this register is allocatable (i.e. don't give out the
5188 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005189 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5190 if (!RC) { // Couldn't allocate this register.
5191 // Reset NumAllocated to make sure we return consecutive registers.
5192 NumAllocated = 0;
5193 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005196 // Okay, this register is good, we can use it.
5197 ++NumAllocated;
5198
5199 // If we allocated enough consecutive registers, succeed.
5200 if (NumAllocated == NumRegs) {
5201 unsigned RegStart = (i-NumAllocated)+1;
5202 unsigned RegEnd = i+1;
5203 // Mark all of the allocated registers used.
5204 for (unsigned i = RegStart; i != RegEnd; ++i)
5205 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005206
5207 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005208 OpInfo.ConstraintVT);
5209 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5210 return;
5211 }
5212 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005213
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005214 // Otherwise, we couldn't allocate enough registers for this.
5215}
5216
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005217/// visitInlineAsm - Handle a call to an InlineAsm object.
5218///
Dan Gohman46510a72010-04-15 01:51:59 +00005219void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5220 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005221
5222 /// ConstraintOperands - Information about all of the constraints.
5223 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005224
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005225 std::set<unsigned> OutputRegs, InputRegs;
5226
5227 // Do a prepass over the constraints, canonicalizing them, and building up the
5228 // ConstraintOperands list.
5229 std::vector<InlineAsm::ConstraintInfo>
5230 ConstraintInfos = IA->ParseConstraints();
5231
Evan Chengda43bcf2008-09-24 00:05:32 +00005232 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005233
Chris Lattner6c147292009-04-30 00:48:50 +00005234 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005235
Chris Lattner6c147292009-04-30 00:48:50 +00005236 // We won't need to flush pending loads if this asm doesn't touch
5237 // memory and is nonvolatile.
5238 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005239 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005240 else
5241 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5244 unsigned ResNo = 0; // ResNo - The result number of the next output.
5245 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5246 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5247 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005248
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005250
5251 // Compute the value type for each operand.
5252 switch (OpInfo.Type) {
5253 case InlineAsm::isOutput:
5254 // Indirect outputs just consume an argument.
5255 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005256 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 break;
5258 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005260 // The return value of the call is this value. As such, there is no
5261 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005262 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005263 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005264 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5265 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5266 } else {
5267 assert(ResNo == 0 && "Asm only has one result!");
5268 OpVT = TLI.getValueType(CS.getType());
5269 }
5270 ++ResNo;
5271 break;
5272 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005273 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005274 break;
5275 case InlineAsm::isClobber:
5276 // Nothing to do.
5277 break;
5278 }
5279
5280 // If this is an input or an indirect output, process the call argument.
5281 // BasicBlocks are labels, currently appearing only in asm's.
5282 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005283 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005284 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5285
Dan Gohman46510a72010-04-15 01:51:59 +00005286 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005287 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005288 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005289 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005290 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005291
Owen Anderson1d0be152009-08-13 21:58:54 +00005292 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005295 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005296 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005297
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005298 // Second pass over the constraints: compute which constraint option to use
5299 // and assign registers to constraints that want a specific physreg.
5300 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5301 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005302
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005303 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005304 // matching input. If their types mismatch, e.g. one is an integer, the
5305 // other is floating point, or their sizes are different, flag it as an
5306 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005307 if (OpInfo.hasMatchingInput()) {
5308 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005309
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005310 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005311 if ((OpInfo.ConstraintVT.isInteger() !=
5312 Input.ConstraintVT.isInteger()) ||
5313 (OpInfo.ConstraintVT.getSizeInBits() !=
5314 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005315 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005316 " with a matching output constraint of"
5317 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005318 }
5319 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005320 }
5321 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005322
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005324 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005326 // If this is a memory input, and if the operand is not indirect, do what we
5327 // need to to provide an address for the memory input.
5328 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5329 !OpInfo.isIndirect) {
5330 assert(OpInfo.Type == InlineAsm::isInput &&
5331 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005332
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 // Memory operands really want the address of the value. If we don't have
5334 // an indirect input, put it in the constpool if we can, otherwise spill
5335 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005336
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005337 // If the operand is a float, integer, or vector constant, spill to a
5338 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005339 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005340 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5341 isa<ConstantVector>(OpVal)) {
5342 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5343 TLI.getPointerTy());
5344 } else {
5345 // Otherwise, create a stack slot and emit a store to it before the
5346 // asm.
5347 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005348 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005349 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5350 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005351 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005353 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005354 OpInfo.CallOperand, StackSlot, NULL, 0,
5355 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005356 OpInfo.CallOperand = StackSlot;
5357 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005359 // There is no longer a Value* corresponding to this operand.
5360 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362 // It is now an indirect operand.
5363 OpInfo.isIndirect = true;
5364 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005365
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005366 // If this constraint is for a specific register, allocate it before
5367 // anything else.
5368 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005369 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005370 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005371
Bill Wendling651ad132009-12-22 01:25:10 +00005372 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005373
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005374 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005375 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005376 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5377 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005378
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005379 // C_Register operands have already been allocated, Other/Memory don't need
5380 // to be.
5381 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005382 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005383 }
5384
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5386 std::vector<SDValue> AsmNodeOperands;
5387 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5388 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005389 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5390 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005391
Chris Lattnerdecc2672010-04-07 05:20:54 +00005392 // If we have a !srcloc metadata node associated with it, we want to attach
5393 // this to the ultimately generated inline asm machineinstr. To do this, we
5394 // pass in the third operand as this (potentially null) inline asm MDNode.
5395 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5396 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005397
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005398 // Loop over all of the inputs, copying the operand values into the
5399 // appropriate registers and processing the output regs.
5400 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005401
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5403 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005404
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005405 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5406 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5407
5408 switch (OpInfo.Type) {
5409 case InlineAsm::isOutput: {
5410 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5411 OpInfo.ConstraintType != TargetLowering::C_Register) {
5412 // Memory output, or 'other' output (e.g. 'X' constraint).
5413 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5414
5415 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005416 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5417 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418 TLI.getPointerTy()));
5419 AsmNodeOperands.push_back(OpInfo.CallOperand);
5420 break;
5421 }
5422
5423 // Otherwise, this is a register or register class output.
5424
5425 // Copy the output from the appropriate register. Find a register that
5426 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005427 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005428 report_fatal_error("Couldn't allocate output reg for constraint '" +
5429 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005430
5431 // If this is an indirect operand, store through the pointer after the
5432 // asm.
5433 if (OpInfo.isIndirect) {
5434 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5435 OpInfo.CallOperandVal));
5436 } else {
5437 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005438 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005439 // Concatenate this output onto the outputs list.
5440 RetValRegs.append(OpInfo.AssignedRegs);
5441 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005442
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005443 // Add information to the INLINEASM node to know that this register is
5444 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005445 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005446 InlineAsm::Kind_RegDefEarlyClobber :
5447 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005448 false,
5449 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005450 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005451 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452 break;
5453 }
5454 case InlineAsm::isInput: {
5455 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005456
Chris Lattner6bdcda32008-10-17 16:47:46 +00005457 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005458 // If this is required to match an output register we have already set,
5459 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005460 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005462 // Scan until we find the definition we already emitted of this operand.
5463 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005464 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465 for (; OperandNo; --OperandNo) {
5466 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005467 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005468 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005469 assert((InlineAsm::isRegDefKind(OpFlag) ||
5470 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5471 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005472 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005473 }
5474
Evan Cheng697cbbf2009-03-20 18:03:34 +00005475 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005476 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005477 if (InlineAsm::isRegDefKind(OpFlag) ||
5478 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005479 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005480 if (OpInfo.isIndirect) {
5481 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005482 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005483 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5484 " don't know how to handle tied "
5485 "indirect register inputs");
5486 }
5487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005488 RegsForValue MatchedRegs;
5489 MatchedRegs.TLI = &TLI;
5490 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005491 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005492 MatchedRegs.RegVTs.push_back(RegVT);
5493 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005494 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005495 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005496 MatchedRegs.Regs.push_back
5497 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005498
5499 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005500 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005501 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005502 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005503 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005504 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005506 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005507
5508 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5509 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5510 "Unexpected number of operands");
5511 // Add information to the INLINEASM node to know about this input.
5512 // See InlineAsm.h isUseOperandTiedToDef.
5513 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5514 OpInfo.getMatchedOperand());
5515 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5516 TLI.getPointerTy()));
5517 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5518 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005519 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005520
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005521 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005522 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005523 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005524
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005525 std::vector<SDValue> Ops;
5526 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005527 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005528 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005529 report_fatal_error("Invalid operand for inline asm constraint '" +
5530 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005532 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005533 unsigned ResOpType =
5534 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005535 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005536 TLI.getPointerTy()));
5537 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5538 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005539 }
5540
5541 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005542 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5543 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5544 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005545
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005546 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005547 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005548 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549 TLI.getPointerTy()));
5550 AsmNodeOperands.push_back(InOperandVal);
5551 break;
5552 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5555 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5556 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005557 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005558 "Don't know how to handle indirect register inputs yet!");
5559
5560 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005561 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005562 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005563 report_fatal_error("Couldn't allocate input reg for constraint '" +
5564 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005565
Dale Johannesen66978ee2009-01-31 02:22:37 +00005566 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005567 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005568
Chris Lattnerdecc2672010-04-07 05:20:54 +00005569 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005570 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 break;
5572 }
5573 case InlineAsm::isClobber: {
5574 // Add the clobbered value to the operand list, so that the register
5575 // allocator is aware that the physreg got clobbered.
5576 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005577 OpInfo.AssignedRegs.AddInlineAsmOperands(
5578 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005579 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005580 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005581 break;
5582 }
5583 }
5584 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005585
Chris Lattnerdecc2672010-04-07 05:20:54 +00005586 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005587 AsmNodeOperands[0] = Chain;
5588 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005589
Dale Johannesen66978ee2009-01-31 02:22:37 +00005590 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005591 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005592 &AsmNodeOperands[0], AsmNodeOperands.size());
5593 Flag = Chain.getValue(1);
5594
5595 // If this asm returns a register value, copy the result from that register
5596 // and set it as the value of the call.
5597 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005598 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005599 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005600
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005601 // FIXME: Why don't we do this for inline asms with MRVs?
5602 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005603 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005604
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005605 // If any of the results of the inline asm is a vector, it may have the
5606 // wrong width/num elts. This can happen for register classes that can
5607 // contain multiple different value types. The preg or vreg allocated may
5608 // not have the same VT as was expected. Convert it to the right type
5609 // with bit_convert.
5610 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005611 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005612 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005613
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005614 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005615 ResultType.isInteger() && Val.getValueType().isInteger()) {
5616 // If a result value was tied to an input value, the computed result may
5617 // have a wider width than the expected result. Extract the relevant
5618 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005619 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005620 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005621
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005622 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005623 }
Dan Gohman95915732008-10-18 01:03:45 +00005624
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005625 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005626 // Don't need to use this as a chain in this case.
5627 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5628 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005629 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005630
Dan Gohman46510a72010-04-15 01:51:59 +00005631 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005632
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005633 // Process indirect outputs, first output all of the flagged copies out of
5634 // physregs.
5635 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5636 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005637 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005638 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005639 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005640 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5641 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643 // Emit the non-flagged stores from the physregs.
5644 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005645 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5646 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5647 StoresToEmit[i].first,
5648 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005649 StoresToEmit[i].second, 0,
5650 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005651 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005652 }
5653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005657
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005658 DAG.setRoot(Chain);
5659}
5660
Dan Gohman46510a72010-04-15 01:51:59 +00005661void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005662 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5663 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005664 getValue(I.getOperand(1)),
5665 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005666}
5667
Dan Gohman46510a72010-04-15 01:51:59 +00005668void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005669 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5670 getRoot(), getValue(I.getOperand(0)),
5671 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005672 setValue(&I, V);
5673 DAG.setRoot(V.getValue(1));
5674}
5675
Dan Gohman46510a72010-04-15 01:51:59 +00005676void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005677 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5678 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005679 getValue(I.getOperand(1)),
5680 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681}
5682
Dan Gohman46510a72010-04-15 01:51:59 +00005683void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005684 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5685 MVT::Other, getRoot(),
5686 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005687 getValue(I.getOperand(2)),
5688 DAG.getSrcValue(I.getOperand(1)),
5689 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005690}
5691
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005692/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005693/// implementation, which just calls LowerCall.
5694/// FIXME: When all targets are
5695/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005696std::pair<SDValue, SDValue>
5697TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5698 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005699 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005700 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005701 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005703 ArgListTy &Args, SelectionDAG &DAG,
5704 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005705 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005706 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005707 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005708 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5710 for (unsigned Value = 0, NumValues = ValueVTs.size();
5711 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005712 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005713 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005714 SDValue Op = SDValue(Args[i].Node.getNode(),
5715 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005716 ISD::ArgFlagsTy Flags;
5717 unsigned OriginalAlignment =
5718 getTargetData()->getABITypeAlignment(ArgTy);
5719
5720 if (Args[i].isZExt)
5721 Flags.setZExt();
5722 if (Args[i].isSExt)
5723 Flags.setSExt();
5724 if (Args[i].isInReg)
5725 Flags.setInReg();
5726 if (Args[i].isSRet)
5727 Flags.setSRet();
5728 if (Args[i].isByVal) {
5729 Flags.setByVal();
5730 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5731 const Type *ElementTy = Ty->getElementType();
5732 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005733 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005734 // For ByVal, alignment should come from FE. BE will guess if this
5735 // info is not there but there are cases it cannot get right.
5736 if (Args[i].Alignment)
5737 FrameAlign = Args[i].Alignment;
5738 Flags.setByValAlign(FrameAlign);
5739 Flags.setByValSize(FrameSize);
5740 }
5741 if (Args[i].isNest)
5742 Flags.setNest();
5743 Flags.setOrigAlign(OriginalAlignment);
5744
Owen Anderson23b9b192009-08-12 00:36:31 +00005745 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5746 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005747 SmallVector<SDValue, 4> Parts(NumParts);
5748 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5749
5750 if (Args[i].isSExt)
5751 ExtendKind = ISD::SIGN_EXTEND;
5752 else if (Args[i].isZExt)
5753 ExtendKind = ISD::ZERO_EXTEND;
5754
Bill Wendling46ada192010-03-02 01:55:18 +00005755 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005756 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005757
Dan Gohman98ca4f22009-08-05 01:29:28 +00005758 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005760 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5761 if (NumParts > 1 && j == 0)
5762 MyFlags.Flags.setSplit();
5763 else if (j != 0)
5764 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005765
Dan Gohman98ca4f22009-08-05 01:29:28 +00005766 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005767 }
5768 }
5769 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005770
Dan Gohman98ca4f22009-08-05 01:29:28 +00005771 // Handle the incoming return values from the call.
5772 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005773 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005775 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005776 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005777 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5778 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005779 for (unsigned i = 0; i != NumRegs; ++i) {
5780 ISD::InputArg MyFlags;
5781 MyFlags.VT = RegisterVT;
5782 MyFlags.Used = isReturnValueUsed;
5783 if (RetSExt)
5784 MyFlags.Flags.setSExt();
5785 if (RetZExt)
5786 MyFlags.Flags.setZExt();
5787 if (isInreg)
5788 MyFlags.Flags.setInReg();
5789 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005790 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005791 }
5792
Dan Gohman98ca4f22009-08-05 01:29:28 +00005793 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005794 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005795 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005796
5797 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005799 "LowerCall didn't return a valid chain!");
5800 assert((!isTailCall || InVals.empty()) &&
5801 "LowerCall emitted a return value for a tail call!");
5802 assert((isTailCall || InVals.size() == Ins.size()) &&
5803 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005804
5805 // For a tail call, the return value is merely live-out and there aren't
5806 // any nodes in the DAG representing it. Return a special value to
5807 // indicate that a tail call has been emitted and no more Instructions
5808 // should be processed in the current block.
5809 if (isTailCall) {
5810 DAG.setRoot(Chain);
5811 return std::make_pair(SDValue(), SDValue());
5812 }
5813
Evan Chengaf1871f2010-03-11 19:38:18 +00005814 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5815 assert(InVals[i].getNode() &&
5816 "LowerCall emitted a null value!");
5817 assert(Ins[i].VT == InVals[i].getValueType() &&
5818 "LowerCall emitted a value with the wrong type!");
5819 });
5820
Dan Gohman98ca4f22009-08-05 01:29:28 +00005821 // Collect the legal value parts into potentially illegal values
5822 // that correspond to the original function's return values.
5823 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5824 if (RetSExt)
5825 AssertOp = ISD::AssertSext;
5826 else if (RetZExt)
5827 AssertOp = ISD::AssertZext;
5828 SmallVector<SDValue, 4> ReturnValues;
5829 unsigned CurReg = 0;
5830 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005831 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005832 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5833 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005834
Bill Wendling46ada192010-03-02 01:55:18 +00005835 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005836 NumRegs, RegisterVT, VT,
5837 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005838 CurReg += NumRegs;
5839 }
5840
5841 // For a function returning void, there is no return value. We can't create
5842 // such a node, so we just return a null return value in that case. In
5843 // that case, nothing will actualy look at the value.
5844 if (ReturnValues.empty())
5845 return std::make_pair(SDValue(), Chain);
5846
5847 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5848 DAG.getVTList(&RetTys[0], RetTys.size()),
5849 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005850 return std::make_pair(Res, Chain);
5851}
5852
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005853void TargetLowering::LowerOperationWrapper(SDNode *N,
5854 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005855 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005856 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005857 if (Res.getNode())
5858 Results.push_back(Res);
5859}
5860
Dan Gohmand858e902010-04-17 15:26:15 +00005861SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005862 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005863 return SDValue();
5864}
5865
Dan Gohman46510a72010-04-15 01:51:59 +00005866void
5867SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005868 SDValue Op = getValue(V);
5869 assert((Op.getOpcode() != ISD::CopyFromReg ||
5870 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5871 "Copy from a reg to the same reg!");
5872 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5873
Owen Anderson23b9b192009-08-12 00:36:31 +00005874 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005876 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005877 PendingExports.push_back(Chain);
5878}
5879
5880#include "llvm/CodeGen/SelectionDAGISel.h"
5881
Dan Gohman46510a72010-04-15 01:51:59 +00005882void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005883 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005884 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005885 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005886 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005887 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005888 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005889 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005890
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005891 // Check whether the function can return without sret-demotion.
5892 SmallVector<EVT, 4> OutVTs;
5893 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005894 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005895 OutVTs, OutsFlags, TLI);
5896 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5897
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005898 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005899 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005900 if (!FLI.CanLowerReturn) {
5901 // Put in an sret pointer parameter before all the other parameters.
5902 SmallVector<EVT, 1> ValueVTs;
5903 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5904
5905 // NOTE: Assuming that a pointer will never break down to more than one VT
5906 // or one register.
5907 ISD::ArgFlagsTy Flags;
5908 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005909 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005910 ISD::InputArg RetArg(Flags, RegisterVT, true);
5911 Ins.push_back(RetArg);
5912 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005913
Dan Gohman98ca4f22009-08-05 01:29:28 +00005914 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005915 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005916 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005917 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005918 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005919 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5920 bool isArgValueUsed = !I->use_empty();
5921 for (unsigned Value = 0, NumValues = ValueVTs.size();
5922 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005923 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005924 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005925 ISD::ArgFlagsTy Flags;
5926 unsigned OriginalAlignment =
5927 TD->getABITypeAlignment(ArgTy);
5928
5929 if (F.paramHasAttr(Idx, Attribute::ZExt))
5930 Flags.setZExt();
5931 if (F.paramHasAttr(Idx, Attribute::SExt))
5932 Flags.setSExt();
5933 if (F.paramHasAttr(Idx, Attribute::InReg))
5934 Flags.setInReg();
5935 if (F.paramHasAttr(Idx, Attribute::StructRet))
5936 Flags.setSRet();
5937 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5938 Flags.setByVal();
5939 const PointerType *Ty = cast<PointerType>(I->getType());
5940 const Type *ElementTy = Ty->getElementType();
5941 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5942 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5943 // For ByVal, alignment should be passed from FE. BE will guess if
5944 // this info is not there but there are cases it cannot get right.
5945 if (F.getParamAlignment(Idx))
5946 FrameAlign = F.getParamAlignment(Idx);
5947 Flags.setByValAlign(FrameAlign);
5948 Flags.setByValSize(FrameSize);
5949 }
5950 if (F.paramHasAttr(Idx, Attribute::Nest))
5951 Flags.setNest();
5952 Flags.setOrigAlign(OriginalAlignment);
5953
Owen Anderson23b9b192009-08-12 00:36:31 +00005954 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5955 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005956 for (unsigned i = 0; i != NumRegs; ++i) {
5957 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5958 if (NumRegs > 1 && i == 0)
5959 MyFlags.Flags.setSplit();
5960 // if it isn't first piece, alignment must be 1
5961 else if (i > 0)
5962 MyFlags.Flags.setOrigAlign(1);
5963 Ins.push_back(MyFlags);
5964 }
5965 }
5966 }
5967
5968 // Call the target to set up the argument values.
5969 SmallVector<SDValue, 8> InVals;
5970 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5971 F.isVarArg(), Ins,
5972 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005973
5974 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005975 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005976 "LowerFormalArguments didn't return a valid chain!");
5977 assert(InVals.size() == Ins.size() &&
5978 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005979 DEBUG({
5980 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5981 assert(InVals[i].getNode() &&
5982 "LowerFormalArguments emitted a null value!");
5983 assert(Ins[i].VT == InVals[i].getValueType() &&
5984 "LowerFormalArguments emitted a value with the wrong type!");
5985 }
5986 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005987
Dan Gohman5e866062009-08-06 15:37:27 +00005988 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005989 DAG.setRoot(NewRoot);
5990
5991 // Set up the argument values.
5992 unsigned i = 0;
5993 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005994 if (!FLI.CanLowerReturn) {
5995 // Create a virtual register for the sret pointer, and put in a copy
5996 // from the sret argument into it.
5997 SmallVector<EVT, 1> ValueVTs;
5998 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5999 EVT VT = ValueVTs[0];
6000 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6001 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006002 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006003 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006004
Dan Gohman2048b852009-11-23 18:04:58 +00006005 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006006 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6007 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6008 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006009 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6010 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006011 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006012
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006013 // i indexes lowered arguments. Bump it past the hidden sret argument.
6014 // Idx indexes LLVM arguments. Don't touch it.
6015 ++i;
6016 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006017
Dan Gohman46510a72010-04-15 01:51:59 +00006018 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006019 ++I, ++Idx) {
6020 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006021 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006022 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006023 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006024 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006025 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006026 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6027 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006028
6029 if (!I->use_empty()) {
6030 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6031 if (F.paramHasAttr(Idx, Attribute::SExt))
6032 AssertOp = ISD::AssertSext;
6033 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6034 AssertOp = ISD::AssertZext;
6035
Bill Wendling46ada192010-03-02 01:55:18 +00006036 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006037 NumParts, PartVT, VT,
6038 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006039 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006040
Dan Gohman98ca4f22009-08-05 01:29:28 +00006041 i += NumParts;
6042 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006043
Dan Gohman98ca4f22009-08-05 01:29:28 +00006044 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00006045 SDValue Res;
6046 if (!ArgValues.empty())
6047 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6048 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006049 SDB->setValue(I, Res);
6050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006051 // If this argument is live outside of the entry block, insert a copy from
6052 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006053 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006055 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006056
Dan Gohman98ca4f22009-08-05 01:29:28 +00006057 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006058
6059 // Finally, if the target has anything special to do, allow it to do so.
6060 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006061 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006062}
6063
6064/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6065/// ensure constants are generated when needed. Remember the virtual registers
6066/// that need to be added to the Machine PHI nodes as input. We cannot just
6067/// directly add them, because expansion might result in multiple MBB's for one
6068/// BB. As such, the start of the BB might correspond to a different MBB than
6069/// the end.
6070///
6071void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006072SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006073 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006074
6075 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6076
6077 // Check successor nodes' PHI nodes that expect a constant to be available
6078 // from this block.
6079 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006080 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006082 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006084 // If this terminator has multiple identical successors (common for
6085 // switches), only handle each succ once.
6086 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006087
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006088 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006089
6090 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6091 // nodes and Machine PHI nodes, but the incoming operands have not been
6092 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006093 for (BasicBlock::const_iterator I = SuccBB->begin();
6094 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006095 // Ignore dead phi's.
6096 if (PN->use_empty()) continue;
6097
6098 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006099 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006100
Dan Gohman46510a72010-04-15 01:51:59 +00006101 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006102 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006103 if (RegOut == 0) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006104 RegOut = FuncInfo.CreateRegForValue(C);
6105 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 }
6107 Reg = RegOut;
6108 } else {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006109 Reg = FuncInfo.ValueMap[PHIOp];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006110 if (Reg == 0) {
6111 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006112 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006113 "Didn't codegen value into a register!??");
Dan Gohmanf81eca02010-04-22 20:46:50 +00006114 Reg = FuncInfo.CreateRegForValue(PHIOp);
6115 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006116 }
6117 }
6118
6119 // Remember that this register needs to added to the machine PHI node as
6120 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006121 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006122 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6123 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006124 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006125 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006126 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006127 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006128 Reg += NumRegisters;
6129 }
6130 }
6131 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006132 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006133}