blob: 1568dbc4450619a2b6c1918dc8b71bfb3334f210 [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();
Evan Chengfb2e7522009-09-18 21:02:19 +0000548 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000549 DAG.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000550 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000551 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000552}
553
554/// getRoot - Return the current virtual root of the Selection DAG,
555/// flushing any PendingLoad items. This must be done before emitting
556/// a store or any other node that may need to be ordered after any
557/// prior load instructions.
558///
Dan Gohman2048b852009-11-23 18:04:58 +0000559SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000560 if (PendingLoads.empty())
561 return DAG.getRoot();
562
563 if (PendingLoads.size() == 1) {
564 SDValue Root = PendingLoads[0];
565 DAG.setRoot(Root);
566 PendingLoads.clear();
567 return Root;
568 }
569
570 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000572 &PendingLoads[0], PendingLoads.size());
573 PendingLoads.clear();
574 DAG.setRoot(Root);
575 return Root;
576}
577
578/// getControlRoot - Similar to getRoot, but instead of flushing all the
579/// PendingLoad items, flush all the PendingExports items. It is necessary
580/// to do this before emitting a terminator instruction.
581///
Dan Gohman2048b852009-11-23 18:04:58 +0000582SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000583 SDValue Root = DAG.getRoot();
584
585 if (PendingExports.empty())
586 return Root;
587
588 // Turn all of the CopyToReg chains into one factored node.
589 if (Root.getOpcode() != ISD::EntryToken) {
590 unsigned i = 0, e = PendingExports.size();
591 for (; i != e; ++i) {
592 assert(PendingExports[i].getNode()->getNumOperands() > 1);
593 if (PendingExports[i].getNode()->getOperand(0) == Root)
594 break; // Don't add the root if we already indirectly depend on it.
595 }
596
597 if (i == e)
598 PendingExports.push_back(Root);
599 }
600
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000602 &PendingExports[0],
603 PendingExports.size());
604 PendingExports.clear();
605 DAG.setRoot(Root);
606 return Root;
607}
608
Bill Wendling4533cac2010-01-28 21:51:40 +0000609void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
610 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
611 DAG.AssignOrdering(Node, SDNodeOrder);
612
613 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
614 AssignOrderingToNode(Node->getOperand(I).getNode());
615}
616
Dan Gohman46510a72010-04-15 01:51:59 +0000617void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000618 // Set up outgoing PHI node register values before emitting the terminator.
619 if (isa<TerminatorInst>(&I))
620 HandlePHINodesInSuccessorBlocks(I.getParent());
621
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000622 CurDebugLoc = I.getDebugLoc();
623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000624 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000625
Dan Gohman92884f72010-04-20 15:03:56 +0000626 if (!isa<TerminatorInst>(&I) && !HasTailCall)
627 CopyToExportRegsIfNeeded(&I);
628
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000629 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000630}
631
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000632void SelectionDAGBuilder::visitPHI(const PHINode &) {
633 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
634}
635
Dan Gohman46510a72010-04-15 01:51:59 +0000636void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000637 // Note: this doesn't use InstVisitor, because it has to work with
638 // ConstantExpr's in addition to instructions.
639 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000640 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000641 // Build the switch statement using the Instruction.def file.
642#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000643 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000644#include "llvm/Instruction.def"
645 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000646
647 // Assign the ordering to the freshly created DAG nodes.
648 if (NodeMap.count(&I)) {
649 ++SDNodeOrder;
650 AssignOrderingToNode(getValue(&I).getNode());
651 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000652}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000653
Dan Gohman2048b852009-11-23 18:04:58 +0000654SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000655 SDValue &N = NodeMap[V];
656 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000657
Dan Gohman383b5f62010-04-17 15:32:28 +0000658 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000659 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000660
Dan Gohman383b5f62010-04-17 15:32:28 +0000661 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000662 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663
Dan Gohman383b5f62010-04-17 15:32:28 +0000664 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000665 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000666
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000667 if (isa<ConstantPointerNull>(C))
668 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000669
Dan Gohman383b5f62010-04-17 15:32:28 +0000670 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000671 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000672
Nate Begeman9008ca62009-04-27 18:41:29 +0000673 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000674 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675
Dan Gohman383b5f62010-04-17 15:32:28 +0000676 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000677 visit(CE->getOpcode(), *CE);
678 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000679 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680 return N1;
681 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000683 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
684 SmallVector<SDValue, 4> Constants;
685 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
686 OI != OE; ++OI) {
687 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000688 // If the operand is an empty aggregate, there are no values.
689 if (!Val) continue;
690 // Add each leaf value from the operand to the Constants list
691 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000692 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
693 Constants.push_back(SDValue(Val, i));
694 }
Bill Wendling87710f02009-12-21 23:47:40 +0000695
Bill Wendling4533cac2010-01-28 21:51:40 +0000696 return DAG.getMergeValues(&Constants[0], Constants.size(),
697 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000698 }
699
Duncan Sands1df98592010-02-16 11:11:14 +0000700 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000701 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
702 "Unknown struct or array constant!");
703
Owen Andersone50ed302009-08-10 22:56:29 +0000704 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000705 ComputeValueVTs(TLI, C->getType(), ValueVTs);
706 unsigned NumElts = ValueVTs.size();
707 if (NumElts == 0)
708 return SDValue(); // empty struct
709 SmallVector<SDValue, 4> Constants(NumElts);
710 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000711 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000712 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000713 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000714 else if (EltVT.isFloatingPoint())
715 Constants[i] = DAG.getConstantFP(0, EltVT);
716 else
717 Constants[i] = DAG.getConstant(0, EltVT);
718 }
Bill Wendling87710f02009-12-21 23:47:40 +0000719
Bill Wendling4533cac2010-01-28 21:51:40 +0000720 return DAG.getMergeValues(&Constants[0], NumElts,
721 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000722 }
723
Dan Gohman383b5f62010-04-17 15:32:28 +0000724 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000725 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000726
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000727 const VectorType *VecTy = cast<VectorType>(V->getType());
728 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000729
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000730 // Now that we know the number and type of the elements, get that number of
731 // elements into the Ops array based on what kind of constant it is.
732 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000733 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000734 for (unsigned i = 0; i != NumElements; ++i)
735 Ops.push_back(getValue(CP->getOperand(i)));
736 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000737 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000738 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000739
740 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000741 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000742 Op = DAG.getConstantFP(0, EltVT);
743 else
744 Op = DAG.getConstant(0, EltVT);
745 Ops.assign(NumElements, Op);
746 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000748 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000749 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
750 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000751 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000752
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000753 // If this is a static alloca, generate it as the frameindex instead of
754 // computation.
755 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
756 DenseMap<const AllocaInst*, int>::iterator SI =
757 FuncInfo.StaticAllocaMap.find(AI);
758 if (SI != FuncInfo.StaticAllocaMap.end())
759 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
760 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000761
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000762 unsigned InReg = FuncInfo.ValueMap[V];
763 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000764
Owen Anderson23b9b192009-08-12 00:36:31 +0000765 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000766 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000767 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000768}
769
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000770/// Get the EVTs and ArgFlags collections that represent the legalized return
771/// type of the given function. This does not require a DAG or a return value,
772/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000773static void getReturnInfo(const Type* ReturnType,
774 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000775 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000776 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000777 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000778 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000779 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000780 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000781 if (NumValues == 0) return;
782 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000783
784 for (unsigned j = 0, f = NumValues; j != f; ++j) {
785 EVT VT = ValueVTs[j];
786 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000787
788 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000789 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000790 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000791 ExtendKind = ISD::ZERO_EXTEND;
792
793 // FIXME: C calling convention requires the return type to be promoted to
794 // at least 32-bit. But this is not necessary for non-C calling
795 // conventions. The frontend should mark functions whose return values
796 // require promoting with signext or zeroext attributes.
797 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000798 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000799 if (VT.bitsLT(MinVT))
800 VT = MinVT;
801 }
802
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000803 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
804 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000805 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
806 PartVT.getTypeForEVT(ReturnType->getContext()));
807
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000808 // 'inreg' on function refers to return value
809 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000810 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000811 Flags.setInReg();
812
813 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000814 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000815 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000816 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000817 Flags.setZExt();
818
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000819 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000820 OutVTs.push_back(PartVT);
821 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000822 if (Offsets)
823 {
824 Offsets->push_back(Offset);
825 Offset += PartSize;
826 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000827 }
828 }
829}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000830
Dan Gohman46510a72010-04-15 01:51:59 +0000831void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000832 SDValue Chain = getControlRoot();
833 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000834 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000835
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000836 if (!FLI.CanLowerReturn) {
837 unsigned DemoteReg = FLI.DemoteRegister;
838 const Function *F = I.getParent()->getParent();
839
840 // Emit a store of the return value through the virtual register.
841 // Leave Outs empty so that LowerReturn won't try to load return
842 // registers the usual way.
843 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000844 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000845 PtrValueVTs);
846
847 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
848 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000849
Owen Andersone50ed302009-08-10 22:56:29 +0000850 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000851 SmallVector<uint64_t, 4> Offsets;
852 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000853 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000854
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000855 SmallVector<SDValue, 4> Chains(NumValues);
856 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000857 for (unsigned i = 0; i != NumValues; ++i) {
858 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
859 DAG.getConstant(Offsets[i], PtrVT));
860 Chains[i] =
861 DAG.getStore(Chain, getCurDebugLoc(),
862 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000863 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000864 }
865
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000866 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
867 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000868 } else if (I.getNumOperands() != 0) {
869 SmallVector<EVT, 4> ValueVTs;
870 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
871 unsigned NumValues = ValueVTs.size();
872 if (NumValues) {
873 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000874 for (unsigned j = 0, f = NumValues; j != f; ++j) {
875 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000876
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000877 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000878
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000879 const Function *F = I.getParent()->getParent();
880 if (F->paramHasAttr(0, Attribute::SExt))
881 ExtendKind = ISD::SIGN_EXTEND;
882 else if (F->paramHasAttr(0, Attribute::ZExt))
883 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000884
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000885 // FIXME: C calling convention requires the return type to be promoted
886 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000887 // conventions. The frontend should mark functions whose return values
888 // require promoting with signext or zeroext attributes.
889 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
890 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
891 if (VT.bitsLT(MinVT))
892 VT = MinVT;
893 }
894
895 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
896 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
897 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000898 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000899 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
900 &Parts[0], NumParts, PartVT, ExtendKind);
901
902 // 'inreg' on function refers to return value
903 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
904 if (F->paramHasAttr(0, Attribute::InReg))
905 Flags.setInReg();
906
907 // Propagate extension type if any
908 if (F->paramHasAttr(0, Attribute::SExt))
909 Flags.setSExt();
910 else if (F->paramHasAttr(0, Attribute::ZExt))
911 Flags.setZExt();
912
913 for (unsigned i = 0; i < NumParts; ++i)
914 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000915 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000916 }
917 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000918
919 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000920 CallingConv::ID CallConv =
921 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000922 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
923 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000924
925 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000927 "LowerReturn didn't return a valid chain!");
928
929 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000930 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000931}
932
Dan Gohmanad62f532009-04-23 23:13:24 +0000933/// CopyToExportRegsIfNeeded - If the given value has virtual registers
934/// created for it, emit nodes to copy the value into the virtual
935/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000936void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000937 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
938 if (VMI != FuncInfo.ValueMap.end()) {
939 assert(!V->use_empty() && "Unused value assigned virtual registers!");
940 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000941 }
942}
943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000944/// ExportFromCurrentBlock - If this condition isn't known to be exported from
945/// the current basic block, add it to ValueMap now so that we'll get a
946/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000947void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000948 // No need to export constants.
949 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000951 // Already exported?
952 if (FuncInfo.isExportedInst(V)) return;
953
954 unsigned Reg = FuncInfo.InitializeRegForValue(V);
955 CopyValueToVirtualRegister(V, Reg);
956}
957
Dan Gohman46510a72010-04-15 01:51:59 +0000958bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000959 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000960 // The operands of the setcc have to be in this block. We don't know
961 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000962 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000963 // Can export from current BB.
964 if (VI->getParent() == FromBB)
965 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000967 // Is already exported, noop.
968 return FuncInfo.isExportedInst(V);
969 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000970
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000971 // If this is an argument, we can export it if the BB is the entry block or
972 // if it is already exported.
973 if (isa<Argument>(V)) {
974 if (FromBB == &FromBB->getParent()->getEntryBlock())
975 return true;
976
977 // Otherwise, can only export this if it is already exported.
978 return FuncInfo.isExportedInst(V);
979 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000981 // Otherwise, constants can always be exported.
982 return true;
983}
984
985static bool InBlock(const Value *V, const BasicBlock *BB) {
986 if (const Instruction *I = dyn_cast<Instruction>(V))
987 return I->getParent() == BB;
988 return true;
989}
990
Dan Gohmanc2277342008-10-17 21:16:08 +0000991/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
992/// This function emits a branch and is used at the leaves of an OR or an
993/// AND operator tree.
994///
995void
Dan Gohman46510a72010-04-15 01:51:59 +0000996SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000997 MachineBasicBlock *TBB,
998 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000999 MachineBasicBlock *CurBB,
1000 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001001 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001002
Dan Gohmanc2277342008-10-17 21:16:08 +00001003 // If the leaf of the tree is a comparison, merge the condition into
1004 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001005 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001006 // The operands of the cmp have to be in this block. We don't know
1007 // how to export them from some other block. If this is the first block
1008 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001009 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001010 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1011 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001012 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001013 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001014 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001015 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001016 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001017 } else {
1018 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001019 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001020 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001021
1022 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001023 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1024 SwitchCases.push_back(CB);
1025 return;
1026 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001027 }
1028
1029 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001030 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001031 NULL, TBB, FBB, CurBB);
1032 SwitchCases.push_back(CB);
1033}
1034
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001035/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001036void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001037 MachineBasicBlock *TBB,
1038 MachineBasicBlock *FBB,
1039 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001040 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001041 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001042 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001043 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001044 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001045 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1046 BOp->getParent() != CurBB->getBasicBlock() ||
1047 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1048 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001049 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001050 return;
1051 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001052
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001053 // Create TmpBB after CurBB.
1054 MachineFunction::iterator BBI = CurBB;
1055 MachineFunction &MF = DAG.getMachineFunction();
1056 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1057 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001058
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001059 if (Opc == Instruction::Or) {
1060 // Codegen X | Y as:
1061 // jmp_if_X TBB
1062 // jmp TmpBB
1063 // TmpBB:
1064 // jmp_if_Y TBB
1065 // jmp FBB
1066 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001069 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001072 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001073 } else {
1074 assert(Opc == Instruction::And && "Unknown merge op!");
1075 // Codegen X & Y as:
1076 // jmp_if_X TmpBB
1077 // jmp FBB
1078 // TmpBB:
1079 // jmp_if_Y TBB
1080 // jmp FBB
1081 //
1082 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001084 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001085 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001086
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001088 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001089 }
1090}
1091
1092/// If the set of cases should be emitted as a series of branches, return true.
1093/// If we should emit this as a bunch of and/or'd together conditions, return
1094/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001095bool
Dan Gohman2048b852009-11-23 18:04:58 +00001096SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001097 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001099 // If this is two comparisons of the same values or'd or and'd together, they
1100 // will get folded into a single comparison, so don't emit two blocks.
1101 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1102 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1103 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1104 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1105 return false;
1106 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001107
Chris Lattner133ce872010-01-02 00:00:03 +00001108 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1109 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1110 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1111 Cases[0].CC == Cases[1].CC &&
1112 isa<Constant>(Cases[0].CmpRHS) &&
1113 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1114 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1115 return false;
1116 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1117 return false;
1118 }
1119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001120 return true;
1121}
1122
Dan Gohman46510a72010-04-15 01:51:59 +00001123void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001124 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001126 // Update machine-CFG edges.
1127 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1128
1129 // Figure out which block is immediately after the current one.
1130 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001131 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001132 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001133 NextBlock = BBI;
1134
1135 if (I.isUnconditional()) {
1136 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001137 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001139 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001140 if (Succ0MBB != NextBlock)
1141 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001142 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001143 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001145 return;
1146 }
1147
1148 // If this condition is one of the special cases we handle, do special stuff
1149 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001150 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001151 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1152
1153 // If this is a series of conditions that are or'd or and'd together, emit
1154 // this as a sequence of branches instead of setcc's with and/or operations.
1155 // For example, instead of something like:
1156 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001159 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001160 // or C, F
1161 // jnz foo
1162 // Emit:
1163 // cmp A, B
1164 // je foo
1165 // cmp D, E
1166 // jle foo
1167 //
Dan Gohman46510a72010-04-15 01:51:59 +00001168 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001169 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001170 (BOp->getOpcode() == Instruction::And ||
1171 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001172 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1173 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 // If the compares in later blocks need to use values not currently
1175 // exported from this block, export them now. This block should always
1176 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001177 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001178
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001179 // Allow some cases to be rejected.
1180 if (ShouldEmitAsBranches(SwitchCases)) {
1181 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1182 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1183 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1184 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001186 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001187 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001188 SwitchCases.erase(SwitchCases.begin());
1189 return;
1190 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192 // Okay, we decided not to do this, remove any inserted MBB's and clear
1193 // SwitchCases.
1194 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001195 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001197 SwitchCases.clear();
1198 }
1199 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001200
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001201 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001202 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001203 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 // Use visitSwitchCase to actually insert the fast branch sequence for this
1206 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001207 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001208}
1209
1210/// visitSwitchCase - Emits the necessary code to represent a single node in
1211/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001212void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1213 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001214 SDValue Cond;
1215 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001216 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001217
1218 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 if (CB.CmpMHS == NULL) {
1220 // Fold "(X == true)" to X and "(X == false)" to !X to
1221 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001222 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001223 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001225 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001226 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001228 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231 } else {
1232 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1233
Anton Korobeynikov23218582008-12-23 22:25:27 +00001234 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1235 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236
1237 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001238 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239
1240 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001242 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001243 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001244 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001245 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247 DAG.getConstant(High-Low, VT), ISD::SETULE);
1248 }
1249 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001252 SwitchBB->addSuccessor(CB.TrueBB);
1253 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001254
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001255 // Set NextBlock to be the MBB immediately after the current one, if any.
1256 // This is used to avoid emitting unnecessary branches to the next block.
1257 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001258 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001259 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 // If the lhs block is the next block, invert the condition so that we can
1263 // fall through to the lhs instead of the rhs block.
1264 if (CB.TrueBB == NextBlock) {
1265 std::swap(CB.TrueBB, CB.FalseBB);
1266 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001267 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001268 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001269
Dale Johannesenf5d97892009-02-04 01:48:28 +00001270 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001272 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001273
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001274 // If the branch was constant folded, fix up the CFG.
1275 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001276 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001277 } else {
1278 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001279 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001280 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001281
Bill Wendling4533cac2010-01-28 21:51:40 +00001282 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001283 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1284 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001285 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001286
1287 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288}
1289
1290/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001291void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292 // Emit the code for the jump table
1293 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001294 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001295 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1296 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001297 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001298 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1299 MVT::Other, Index.getValue(1),
1300 Table, Index);
1301 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001302}
1303
1304/// visitJumpTableHeader - This function emits necessary code to produce index
1305/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001306void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001307 JumpTableHeader &JTH,
1308 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001309 // Subtract the lowest switch case value from the value being switched on and
1310 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311 // difference between smallest and largest cases.
1312 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001313 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001314 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001315 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001316
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001317 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001318 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001319 // can be used as an index into the jump table in a subsequent basic block.
1320 // This value may be smaller or larger than the target's pointer type, and
1321 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001322 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001323
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001325 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1326 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327 JT.Reg = JumpTableReg;
1328
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001329 // Emit the range check for the jump table, and branch to the default block
1330 // for the switch statement if the value being switched on exceeds the largest
1331 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001332 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001333 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001334 DAG.getConstant(JTH.Last-JTH.First,VT),
1335 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336
1337 // Set NextBlock to be the MBB immediately after the current one, if any.
1338 // This is used to avoid emitting unnecessary branches to the next block.
1339 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001340 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001341
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001342 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001343 NextBlock = BBI;
1344
Dale Johannesen66978ee2009-01-31 02:22:37 +00001345 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001346 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001347 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001348
Bill Wendling4533cac2010-01-28 21:51:40 +00001349 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001350 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1351 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001352
Bill Wendling87710f02009-12-21 23:47:40 +00001353 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001354}
1355
1356/// visitBitTestHeader - This function emits necessary code to produce value
1357/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001358void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1359 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001360 // Subtract the minimum value
1361 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001362 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001363 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001364 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365
1366 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001367 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001368 TLI.getSetCCResultType(Sub.getValueType()),
1369 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001370 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001371
Bill Wendling87710f02009-12-21 23:47:40 +00001372 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1373 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374
Duncan Sands92abc622009-01-31 15:50:11 +00001375 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001376 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1377 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378
1379 // Set NextBlock to be the MBB immediately after the current one, if any.
1380 // This is used to avoid emitting unnecessary branches to the next block.
1381 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001382 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001383 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384 NextBlock = BBI;
1385
1386 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1387
Dan Gohman99be8ae2010-04-19 22:41:47 +00001388 SwitchBB->addSuccessor(B.Default);
1389 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001390
Dale Johannesen66978ee2009-01-31 02:22:37 +00001391 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001393 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001394
Bill Wendling4533cac2010-01-28 21:51:40 +00001395 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001396 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1397 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001398
Bill Wendling87710f02009-12-21 23:47:40 +00001399 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001400}
1401
1402/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001403void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1404 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001405 BitTestCase &B,
1406 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001407 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001408 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001409 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001410 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001411 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001412 DAG.getConstant(1, TLI.getPointerTy()),
1413 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001414
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001415 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001416 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001417 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001418 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001419 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1420 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001421 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001422 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001423
Dan Gohman99be8ae2010-04-19 22:41:47 +00001424 SwitchBB->addSuccessor(B.TargetBB);
1425 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001426
Dale Johannesen66978ee2009-01-31 02:22:37 +00001427 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001428 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001429 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430
1431 // Set NextBlock to be the MBB immediately after the current one, if any.
1432 // This is used to avoid emitting unnecessary branches to the next block.
1433 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001434 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001435 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 NextBlock = BBI;
1437
Bill Wendling4533cac2010-01-28 21:51:40 +00001438 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001439 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1440 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001441
Bill Wendling87710f02009-12-21 23:47:40 +00001442 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001443}
1444
Dan Gohman46510a72010-04-15 01:51:59 +00001445void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001446 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001448 // Retrieve successors.
1449 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1450 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1451
Gabor Greifb67e6b32009-01-15 11:10:44 +00001452 const Value *Callee(I.getCalledValue());
1453 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 visitInlineAsm(&I);
1455 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001456 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457
1458 // If the value of the invoke is used outside of its defining block, make it
1459 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001460 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001461
1462 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001463 InvokeMBB->addSuccessor(Return);
1464 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001465
1466 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001467 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1468 MVT::Other, getControlRoot(),
1469 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470}
1471
Dan Gohman46510a72010-04-15 01:51:59 +00001472void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473}
1474
1475/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1476/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001477bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1478 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001479 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001480 MachineBasicBlock *Default,
1481 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001483
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001484 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001485 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001487 return false;
1488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 // Get the MachineFunction which holds the current MBB. This is used when
1490 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001491 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001492
1493 // Figure out which block is immediately after the current one.
1494 MachineBasicBlock *NextBlock = 0;
1495 MachineFunction::iterator BBI = CR.CaseBB;
1496
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001497 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 NextBlock = BBI;
1499
1500 // TODO: If any two of the cases has the same destination, and if one value
1501 // is the same as the other, but has one bit unset that the other has set,
1502 // use bit manipulation to do two compares at once. For example:
1503 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001505 // Rearrange the case blocks so that the last one falls through if possible.
1506 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1507 // The last case block won't fall through into 'NextBlock' if we emit the
1508 // branches in this order. See if rearranging a case value would help.
1509 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1510 if (I->BB == NextBlock) {
1511 std::swap(*I, BackCase);
1512 break;
1513 }
1514 }
1515 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001516
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001517 // Create a CaseBlock record representing a conditional branch to
1518 // the Case's target mbb if the value being switched on SV is equal
1519 // to C.
1520 MachineBasicBlock *CurBlock = CR.CaseBB;
1521 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1522 MachineBasicBlock *FallThrough;
1523 if (I != E-1) {
1524 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1525 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001526
1527 // Put SV in a virtual register to make it available from the new blocks.
1528 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001529 } else {
1530 // If the last case doesn't match, go to the default block.
1531 FallThrough = Default;
1532 }
1533
Dan Gohman46510a72010-04-15 01:51:59 +00001534 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001535 ISD::CondCode CC;
1536 if (I->High == I->Low) {
1537 // This is just small small case range :) containing exactly 1 case
1538 CC = ISD::SETEQ;
1539 LHS = SV; RHS = I->High; MHS = NULL;
1540 } else {
1541 CC = ISD::SETLE;
1542 LHS = I->Low; MHS = SV; RHS = I->High;
1543 }
1544 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001545
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 // If emitting the first comparison, just call visitSwitchCase to emit the
1547 // code into the current block. Otherwise, push the CaseBlock onto the
1548 // vector to be later processed by SDISel, and insert the node's MBB
1549 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001550 if (CurBlock == SwitchBB)
1551 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552 else
1553 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001555 CurBlock = FallThrough;
1556 }
1557
1558 return true;
1559}
1560
1561static inline bool areJTsAllowed(const TargetLowering &TLI) {
1562 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1564 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001565}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001566
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001567static APInt ComputeRange(const APInt &First, const APInt &Last) {
1568 APInt LastExt(Last), FirstExt(First);
1569 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1570 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1571 return (LastExt - FirstExt + 1ULL);
1572}
1573
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001574/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001575bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1576 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001577 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001578 MachineBasicBlock* Default,
1579 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 Case& FrontCase = *CR.Range.first;
1581 Case& BackCase = *(CR.Range.second-1);
1582
Chris Lattnere880efe2009-11-07 07:50:34 +00001583 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1584 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585
Chris Lattnere880efe2009-11-07 07:50:34 +00001586 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001587 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1588 I!=E; ++I)
1589 TSize += I->size();
1590
Dan Gohmane0567812010-04-08 23:03:40 +00001591 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001592 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001593
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001594 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001595 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596 if (Density < 0.4)
1597 return false;
1598
David Greene4b69d992010-01-05 01:24:57 +00001599 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001600 << "First entry: " << First << ". Last entry: " << Last << '\n'
1601 << "Range: " << Range
1602 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001603
1604 // Get the MachineFunction which holds the current MBB. This is used when
1605 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001606 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607
1608 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001610 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001611
1612 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1613
1614 // Create a new basic block to hold the code for loading the address
1615 // of the jump table, and jumping to it. Update successor information;
1616 // we will either branch to the default case for the switch, or the jump
1617 // table.
1618 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1619 CurMF->insert(BBI, JumpTableBB);
1620 CR.CaseBB->addSuccessor(Default);
1621 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001622
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001623 // Build a vector of destination BBs, corresponding to each target
1624 // of the jump table. If the value of the jump table slot corresponds to
1625 // a case statement, push the case's BB onto the vector, otherwise, push
1626 // the default BB.
1627 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001628 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001630 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1631 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001632
1633 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634 DestBBs.push_back(I->BB);
1635 if (TEI==High)
1636 ++I;
1637 } else {
1638 DestBBs.push_back(Default);
1639 }
1640 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001641
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001642 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001643 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1644 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001645 E = DestBBs.end(); I != E; ++I) {
1646 if (!SuccsHandled[(*I)->getNumber()]) {
1647 SuccsHandled[(*I)->getNumber()] = true;
1648 JumpTableBB->addSuccessor(*I);
1649 }
1650 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001651
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001652 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001653 unsigned JTEncoding = TLI.getJumpTableEncoding();
1654 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001655 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001656
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001657 // Set the jump table information so that we can codegen it as a second
1658 // MachineBasicBlock
1659 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001660 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1661 if (CR.CaseBB == SwitchBB)
1662 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001664 JTCases.push_back(JumpTableBlock(JTH, JT));
1665
1666 return true;
1667}
1668
1669/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1670/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001671bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1672 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001673 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001674 MachineBasicBlock *Default,
1675 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001676 // Get the MachineFunction which holds the current MBB. This is used when
1677 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001678 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679
1680 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001682 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001683
1684 Case& FrontCase = *CR.Range.first;
1685 Case& BackCase = *(CR.Range.second-1);
1686 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1687
1688 // Size is the number of Cases represented by this range.
1689 unsigned Size = CR.Range.second - CR.Range.first;
1690
Chris Lattnere880efe2009-11-07 07:50:34 +00001691 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1692 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001693 double FMetric = 0;
1694 CaseItr Pivot = CR.Range.first + Size/2;
1695
1696 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1697 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001698 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001699 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1700 I!=E; ++I)
1701 TSize += I->size();
1702
Chris Lattnere880efe2009-11-07 07:50:34 +00001703 APInt LSize = FrontCase.size();
1704 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001705 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001706 << "First: " << First << ", Last: " << Last <<'\n'
1707 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1709 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001710 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1711 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001712 APInt Range = ComputeRange(LEnd, RBegin);
1713 assert((Range - 2ULL).isNonNegative() &&
1714 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001715 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001716 (LEnd - First + 1ULL).roundToDouble();
1717 double RDensity = (double)RSize.roundToDouble() /
1718 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001719 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001720 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001721 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001722 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1723 << "LDensity: " << LDensity
1724 << ", RDensity: " << RDensity << '\n'
1725 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 if (FMetric < Metric) {
1727 Pivot = J;
1728 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001729 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 }
1731
1732 LSize += J->size();
1733 RSize -= J->size();
1734 }
1735 if (areJTsAllowed(TLI)) {
1736 // If our case is dense we *really* should handle it earlier!
1737 assert((FMetric > 0) && "Should handle dense range earlier!");
1738 } else {
1739 Pivot = CR.Range.first + Size/2;
1740 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001741
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742 CaseRange LHSR(CR.Range.first, Pivot);
1743 CaseRange RHSR(Pivot, CR.Range.second);
1744 Constant *C = Pivot->Low;
1745 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001746
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001747 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001748 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001749 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001750 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001751 // Pivot's Value, then we can branch directly to the LHS's Target,
1752 // rather than creating a leaf node for it.
1753 if ((LHSR.second - LHSR.first) == 1 &&
1754 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001755 cast<ConstantInt>(C)->getValue() ==
1756 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001757 TrueBB = LHSR.first->BB;
1758 } else {
1759 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1760 CurMF->insert(BBI, TrueBB);
1761 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001762
1763 // Put SV in a virtual register to make it available from the new blocks.
1764 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001765 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001766
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001767 // Similar to the optimization above, if the Value being switched on is
1768 // known to be less than the Constant CR.LT, and the current Case Value
1769 // is CR.LT - 1, then we can branch directly to the target block for
1770 // the current Case Value, rather than emitting a RHS leaf node for it.
1771 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001772 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1773 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001774 FalseBB = RHSR.first->BB;
1775 } else {
1776 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1777 CurMF->insert(BBI, FalseBB);
1778 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001779
1780 // Put SV in a virtual register to make it available from the new blocks.
1781 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001782 }
1783
1784 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001785 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 // Otherwise, branch to LHS.
1787 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1788
Dan Gohman99be8ae2010-04-19 22:41:47 +00001789 if (CR.CaseBB == SwitchBB)
1790 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 else
1792 SwitchCases.push_back(CB);
1793
1794 return true;
1795}
1796
1797/// handleBitTestsSwitchCase - if current case range has few destination and
1798/// range span less, than machine word bitwidth, encode case range into series
1799/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001800bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1801 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001802 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001803 MachineBasicBlock* Default,
1804 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001805 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001806 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001807
1808 Case& FrontCase = *CR.Range.first;
1809 Case& BackCase = *(CR.Range.second-1);
1810
1811 // Get the MachineFunction which holds the current MBB. This is used when
1812 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001813 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001815 // If target does not have legal shift left, do not emit bit tests at all.
1816 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1817 return false;
1818
Anton Korobeynikov23218582008-12-23 22:25:27 +00001819 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001820 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1821 I!=E; ++I) {
1822 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001823 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001824 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 // Count unique destinations
1827 SmallSet<MachineBasicBlock*, 4> Dests;
1828 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1829 Dests.insert(I->BB);
1830 if (Dests.size() > 3)
1831 // Don't bother the code below, if there are too much unique destinations
1832 return false;
1833 }
David Greene4b69d992010-01-05 01:24:57 +00001834 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001835 << Dests.size() << '\n'
1836 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001838 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001839 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1840 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001841 APInt cmpRange = maxValue - minValue;
1842
David Greene4b69d992010-01-05 01:24:57 +00001843 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001844 << "Low bound: " << minValue << '\n'
1845 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001846
Dan Gohmane0567812010-04-08 23:03:40 +00001847 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001848 (!(Dests.size() == 1 && numCmps >= 3) &&
1849 !(Dests.size() == 2 && numCmps >= 5) &&
1850 !(Dests.size() >= 3 && numCmps >= 6)))
1851 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001852
David Greene4b69d992010-01-05 01:24:57 +00001853 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001854 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1855
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001856 // Optimize the case where all the case values fit in a
1857 // word without having to subtract minValue. In this case,
1858 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001859 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001860 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001861 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001862 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001864
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001865 CaseBitsVector CasesBits;
1866 unsigned i, count = 0;
1867
1868 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1869 MachineBasicBlock* Dest = I->BB;
1870 for (i = 0; i < count; ++i)
1871 if (Dest == CasesBits[i].BB)
1872 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874 if (i == count) {
1875 assert((count < 3) && "Too much destinations to test!");
1876 CasesBits.push_back(CaseBits(0, Dest, 0));
1877 count++;
1878 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001879
1880 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1881 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1882
1883 uint64_t lo = (lowValue - lowBound).getZExtValue();
1884 uint64_t hi = (highValue - lowBound).getZExtValue();
1885
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 for (uint64_t j = lo; j <= hi; j++) {
1887 CasesBits[i].Mask |= 1ULL << j;
1888 CasesBits[i].Bits++;
1889 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001890
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891 }
1892 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001893
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894 BitTestInfo BTC;
1895
1896 // Figure out which block is immediately after the current one.
1897 MachineFunction::iterator BBI = CR.CaseBB;
1898 ++BBI;
1899
1900 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1901
David Greene4b69d992010-01-05 01:24:57 +00001902 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001904 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001905 << ", Bits: " << CasesBits[i].Bits
1906 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907
1908 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1909 CurMF->insert(BBI, CaseBB);
1910 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1911 CaseBB,
1912 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001913
1914 // Put SV in a virtual register to make it available from the new blocks.
1915 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001916 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001917
1918 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001919 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001920 CR.CaseBB, Default, BTC);
1921
Dan Gohman99be8ae2010-04-19 22:41:47 +00001922 if (CR.CaseBB == SwitchBB)
1923 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 BitTestCases.push_back(BTB);
1926
1927 return true;
1928}
1929
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001931size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1932 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934
1935 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001936 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001937 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1938 Cases.push_back(Case(SI.getSuccessorValue(i),
1939 SI.getSuccessorValue(i),
1940 SMBB));
1941 }
1942 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1943
1944 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001945 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 // Must recompute end() each iteration because it may be
1947 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001948 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1949 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1950 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001951 MachineBasicBlock* nextBB = J->BB;
1952 MachineBasicBlock* currentBB = I->BB;
1953
1954 // If the two neighboring cases go to the same destination, merge them
1955 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001956 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001957 I->High = J->High;
1958 J = Cases.erase(J);
1959 } else {
1960 I = J++;
1961 }
1962 }
1963
1964 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1965 if (I->Low != I->High)
1966 // A range counts double, since it requires two compares.
1967 ++numCmps;
1968 }
1969
1970 return numCmps;
1971}
1972
Dan Gohman46510a72010-04-15 01:51:59 +00001973void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001974 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1975
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 // Figure out which block is immediately after the current one.
1977 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1979
1980 // If there is only the default destination, branch to it if it is not the
1981 // next basic block. Otherwise, just fall through.
1982 if (SI.getNumOperands() == 2) {
1983 // Update machine-CFG edges.
1984
1985 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001986 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001987 if (Default != NextBlock)
1988 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1989 MVT::Other, getControlRoot(),
1990 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 return;
1993 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001995 // If there are any non-default case statements, create a vector of Cases
1996 // representing each one, and sort the vector so that we can efficiently
1997 // create a binary search tree from them.
1998 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001999 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002000 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002001 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002002 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002003
2004 // Get the Value to be switched on and default basic blocks, which will be
2005 // inserted into CaseBlock records, representing basic blocks in the binary
2006 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002007 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008
2009 // Push the initial CaseRec onto the worklist
2010 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002011 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2012 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013
2014 while (!WorkList.empty()) {
2015 // Grab a record representing a case range to process off the worklist
2016 CaseRec CR = WorkList.back();
2017 WorkList.pop_back();
2018
Dan Gohman99be8ae2010-04-19 22:41:47 +00002019 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002021
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 // If the range has few cases (two or less) emit a series of specific
2023 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002024 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002026
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002027 // If the switch has more than 5 blocks, and at least 40% dense, and the
2028 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002029 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002030 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002033 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2034 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002035 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002036 }
2037}
2038
Dan Gohman46510a72010-04-15 01:51:59 +00002039void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002040 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2041
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002042 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002043 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002044 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002045 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002046 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002047 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002048 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2049 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002050 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002051
Bill Wendling4533cac2010-01-28 21:51:40 +00002052 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2053 MVT::Other, getControlRoot(),
2054 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002055}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056
Dan Gohman46510a72010-04-15 01:51:59 +00002057void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002058 // -0.0 - X --> fneg
2059 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002060 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002061 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2062 const VectorType *DestTy = cast<VectorType>(I.getType());
2063 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002064 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002065 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002066 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002067 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002069 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2070 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002071 return;
2072 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002073 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002074 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002075
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002076 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002077 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002078 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002079 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2080 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002081 return;
2082 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002084 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002085}
2086
Dan Gohman46510a72010-04-15 01:51:59 +00002087void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002088 SDValue Op1 = getValue(I.getOperand(0));
2089 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002090 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2091 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002092}
2093
Dan Gohman46510a72010-04-15 01:51:59 +00002094void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095 SDValue Op1 = getValue(I.getOperand(0));
2096 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002097 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002098 Op2.getValueType() != TLI.getShiftAmountTy()) {
2099 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002100 EVT PTy = TLI.getPointerTy();
2101 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002102 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002103 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2104 TLI.getShiftAmountTy(), Op2);
2105 // If the operand is larger than the shift count type but the shift
2106 // count type has enough bits to represent any shift value, truncate
2107 // it now. This is a common case and it exposes the truncate to
2108 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002109 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002110 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2111 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2112 TLI.getShiftAmountTy(), Op2);
2113 // Otherwise we'll need to temporarily settle for some other
2114 // convenient type; type legalization will make adjustments as
2115 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002116 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002117 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002118 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002119 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002120 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002121 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002123
Bill Wendling4533cac2010-01-28 21:51:40 +00002124 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2125 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126}
2127
Dan Gohman46510a72010-04-15 01:51:59 +00002128void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002130 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002131 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002132 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 predicate = ICmpInst::Predicate(IC->getPredicate());
2134 SDValue Op1 = getValue(I.getOperand(0));
2135 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002136 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002137
Owen Andersone50ed302009-08-10 22:56:29 +00002138 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002139 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002140}
2141
Dan Gohman46510a72010-04-15 01:51:59 +00002142void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002144 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002146 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002147 predicate = FCmpInst::Predicate(FC->getPredicate());
2148 SDValue Op1 = getValue(I.getOperand(0));
2149 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002150 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002151 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002152 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153}
2154
Dan Gohman46510a72010-04-15 01:51:59 +00002155void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002156 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002157 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2158 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002159 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002160
Bill Wendling49fcff82009-12-21 22:30:11 +00002161 SmallVector<SDValue, 4> Values(NumValues);
2162 SDValue Cond = getValue(I.getOperand(0));
2163 SDValue TrueVal = getValue(I.getOperand(1));
2164 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002165
Bill Wendling4533cac2010-01-28 21:51:40 +00002166 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002167 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002168 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2169 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002170 SDValue(TrueVal.getNode(),
2171 TrueVal.getResNo() + i),
2172 SDValue(FalseVal.getNode(),
2173 FalseVal.getResNo() + i));
2174
Bill Wendling4533cac2010-01-28 21:51:40 +00002175 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2176 DAG.getVTList(&ValueVTs[0], NumValues),
2177 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002178}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002179
Dan Gohman46510a72010-04-15 01:51:59 +00002180void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2182 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002183 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002184 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185}
2186
Dan Gohman46510a72010-04-15 01:51:59 +00002187void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2189 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2190 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002191 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002192 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002193}
2194
Dan Gohman46510a72010-04-15 01:51:59 +00002195void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002196 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2197 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2198 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002200 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201}
2202
Dan Gohman46510a72010-04-15 01:51:59 +00002203void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002204 // FPTrunc is never a no-op cast, no need to check
2205 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002207 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2208 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002209}
2210
Dan Gohman46510a72010-04-15 01:51:59 +00002211void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212 // FPTrunc is never a no-op cast, no need to check
2213 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002214 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002215 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002216}
2217
Dan Gohman46510a72010-04-15 01:51:59 +00002218void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002219 // FPToUI is never a no-op cast, no need to check
2220 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002221 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002222 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223}
2224
Dan Gohman46510a72010-04-15 01:51:59 +00002225void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 // FPToSI is never a no-op cast, no need to check
2227 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002228 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002229 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002230}
2231
Dan Gohman46510a72010-04-15 01:51:59 +00002232void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002233 // UIToFP is never a no-op cast, no need to check
2234 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002235 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002236 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002237}
2238
Dan Gohman46510a72010-04-15 01:51:59 +00002239void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002240 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002242 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002243 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244}
2245
Dan Gohman46510a72010-04-15 01:51:59 +00002246void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002247 // What to do depends on the size of the integer and the size of the pointer.
2248 // We can either truncate, zero extend, or no-op, accordingly.
2249 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002250 EVT SrcVT = N.getValueType();
2251 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002252 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002253}
2254
Dan Gohman46510a72010-04-15 01:51:59 +00002255void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002256 // What to do depends on the size of the integer and the size of the pointer.
2257 // We can either truncate, zero extend, or no-op, accordingly.
2258 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002259 EVT SrcVT = N.getValueType();
2260 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002261 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002262}
2263
Dan Gohman46510a72010-04-15 01:51:59 +00002264void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002265 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002266 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002267
Bill Wendling49fcff82009-12-21 22:30:11 +00002268 // BitCast assures us that source and destination are the same size so this is
2269 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002270 if (DestVT != N.getValueType())
2271 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2272 DestVT, N)); // convert types.
2273 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002274 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002275}
2276
Dan Gohman46510a72010-04-15 01:51:59 +00002277void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002278 SDValue InVec = getValue(I.getOperand(0));
2279 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002280 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002281 TLI.getPointerTy(),
2282 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002283 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2284 TLI.getValueType(I.getType()),
2285 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002286}
2287
Dan Gohman46510a72010-04-15 01:51:59 +00002288void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002289 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002290 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002291 TLI.getPointerTy(),
2292 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002293 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2294 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295}
2296
Mon P Wangaeb06d22008-11-10 04:46:22 +00002297// Utility for visitShuffleVector - Returns true if the mask is mask starting
2298// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002299static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2300 unsigned MaskNumElts = Mask.size();
2301 for (unsigned i = 0; i != MaskNumElts; ++i)
2302 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002303 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002304 return true;
2305}
2306
Dan Gohman46510a72010-04-15 01:51:59 +00002307void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002308 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002309 SDValue Src1 = getValue(I.getOperand(0));
2310 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002311
Nate Begeman9008ca62009-04-27 18:41:29 +00002312 // Convert the ConstantVector mask operand into an array of ints, with -1
2313 // representing undef values.
2314 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002315 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002316 unsigned MaskNumElts = MaskElts.size();
2317 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002318 if (isa<UndefValue>(MaskElts[i]))
2319 Mask.push_back(-1);
2320 else
2321 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2322 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002323
Owen Andersone50ed302009-08-10 22:56:29 +00002324 EVT VT = TLI.getValueType(I.getType());
2325 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002326 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002327
Mon P Wangc7849c22008-11-16 05:06:27 +00002328 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002329 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2330 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002331 return;
2332 }
2333
2334 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002335 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2336 // Mask is longer than the source vectors and is a multiple of the source
2337 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002338 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002339 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2340 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002341 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2342 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002343 return;
2344 }
2345
Mon P Wangc7849c22008-11-16 05:06:27 +00002346 // Pad both vectors with undefs to make them the same length as the mask.
2347 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002348 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2349 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002350 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002351
Nate Begeman9008ca62009-04-27 18:41:29 +00002352 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2353 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002354 MOps1[0] = Src1;
2355 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002356
2357 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2358 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002359 &MOps1[0], NumConcat);
2360 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002361 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002362 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002363
Mon P Wangaeb06d22008-11-10 04:46:22 +00002364 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002365 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002366 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002367 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002368 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002369 MappedOps.push_back(Idx);
2370 else
2371 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002372 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002373
Bill Wendling4533cac2010-01-28 21:51:40 +00002374 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2375 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002376 return;
2377 }
2378
Mon P Wangc7849c22008-11-16 05:06:27 +00002379 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002380 // Analyze the access pattern of the vector to see if we can extract
2381 // two subvectors and do the shuffle. The analysis is done by calculating
2382 // the range of elements the mask access on both vectors.
2383 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2384 int MaxRange[2] = {-1, -1};
2385
Nate Begeman5a5ca152009-04-29 05:20:52 +00002386 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002387 int Idx = Mask[i];
2388 int Input = 0;
2389 if (Idx < 0)
2390 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002391
Nate Begeman5a5ca152009-04-29 05:20:52 +00002392 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002393 Input = 1;
2394 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002395 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002396 if (Idx > MaxRange[Input])
2397 MaxRange[Input] = Idx;
2398 if (Idx < MinRange[Input])
2399 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002400 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002401
Mon P Wangc7849c22008-11-16 05:06:27 +00002402 // Check if the access is smaller than the vector size and can we find
2403 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002404 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2405 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002406 int StartIdx[2]; // StartIdx to extract from
2407 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002408 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002409 RangeUse[Input] = 0; // Unused
2410 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002411 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002412 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002413 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002414 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002415 RangeUse[Input] = 1; // Extract from beginning of the vector
2416 StartIdx[Input] = 0;
2417 } else {
2418 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002419 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002420 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002421 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002422 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002423 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002424 }
2425
Bill Wendling636e2582009-08-21 18:16:06 +00002426 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002427 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002428 return;
2429 }
2430 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2431 // Extract appropriate subvector and generate a vector shuffle
2432 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002433 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002434 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002435 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002436 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002437 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002438 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002439 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002440
Mon P Wangc7849c22008-11-16 05:06:27 +00002441 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002442 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002443 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002444 int Idx = Mask[i];
2445 if (Idx < 0)
2446 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002447 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002448 MappedOps.push_back(Idx - StartIdx[0]);
2449 else
2450 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002451 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002452
Bill Wendling4533cac2010-01-28 21:51:40 +00002453 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2454 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002455 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002456 }
2457 }
2458
Mon P Wangc7849c22008-11-16 05:06:27 +00002459 // We can't use either concat vectors or extract subvectors so fall back to
2460 // replacing the shuffle with extract and build vector.
2461 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002462 EVT EltVT = VT.getVectorElementType();
2463 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002464 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002465 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002466 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002467 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002468 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002469 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002470 SDValue Res;
2471
Nate Begeman5a5ca152009-04-29 05:20:52 +00002472 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002473 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2474 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002475 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002476 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2477 EltVT, Src2,
2478 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2479
2480 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002481 }
2482 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002483
Bill Wendling4533cac2010-01-28 21:51:40 +00002484 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2485 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486}
2487
Dan Gohman46510a72010-04-15 01:51:59 +00002488void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489 const Value *Op0 = I.getOperand(0);
2490 const Value *Op1 = I.getOperand(1);
2491 const Type *AggTy = I.getType();
2492 const Type *ValTy = Op1->getType();
2493 bool IntoUndef = isa<UndefValue>(Op0);
2494 bool FromUndef = isa<UndefValue>(Op1);
2495
2496 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2497 I.idx_begin(), I.idx_end());
2498
Owen Andersone50ed302009-08-10 22:56:29 +00002499 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002501 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002502 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2503
2504 unsigned NumAggValues = AggValueVTs.size();
2505 unsigned NumValValues = ValValueVTs.size();
2506 SmallVector<SDValue, 4> Values(NumAggValues);
2507
2508 SDValue Agg = getValue(Op0);
2509 SDValue Val = getValue(Op1);
2510 unsigned i = 0;
2511 // Copy the beginning value(s) from the original aggregate.
2512 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002513 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002514 SDValue(Agg.getNode(), Agg.getResNo() + i);
2515 // Copy values from the inserted value(s).
2516 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002517 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002518 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2519 // Copy remaining value(s) from the original aggregate.
2520 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002521 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002522 SDValue(Agg.getNode(), Agg.getResNo() + i);
2523
Bill Wendling4533cac2010-01-28 21:51:40 +00002524 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2525 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2526 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002527}
2528
Dan Gohman46510a72010-04-15 01:51:59 +00002529void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002530 const Value *Op0 = I.getOperand(0);
2531 const Type *AggTy = Op0->getType();
2532 const Type *ValTy = I.getType();
2533 bool OutOfUndef = isa<UndefValue>(Op0);
2534
2535 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2536 I.idx_begin(), I.idx_end());
2537
Owen Andersone50ed302009-08-10 22:56:29 +00002538 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2540
2541 unsigned NumValValues = ValValueVTs.size();
2542 SmallVector<SDValue, 4> Values(NumValValues);
2543
2544 SDValue Agg = getValue(Op0);
2545 // Copy out the selected value(s).
2546 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2547 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002548 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002549 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002550 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002551
Bill Wendling4533cac2010-01-28 21:51:40 +00002552 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2553 DAG.getVTList(&ValValueVTs[0], NumValValues),
2554 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002555}
2556
Dan Gohman46510a72010-04-15 01:51:59 +00002557void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 SDValue N = getValue(I.getOperand(0));
2559 const Type *Ty = I.getOperand(0)->getType();
2560
Dan Gohman46510a72010-04-15 01:51:59 +00002561 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002562 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002563 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002564 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2565 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2566 if (Field) {
2567 // N = N + Offset
2568 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002569 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002570 DAG.getIntPtrConstant(Offset));
2571 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002573 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002574 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2575 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2576
2577 // Offset canonically 0 for unions, but type changes
2578 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002579 } else {
2580 Ty = cast<SequentialType>(Ty)->getElementType();
2581
2582 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002583 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002584 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002585 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002586 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002587 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002588 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002589 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002590 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002591 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2592 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002593 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002594 else
Evan Chengb1032a82009-02-09 20:54:38 +00002595 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002596
Dale Johannesen66978ee2009-01-31 02:22:37 +00002597 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002598 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002599 continue;
2600 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002601
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002602 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002603 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2604 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002605 SDValue IdxN = getValue(Idx);
2606
2607 // If the index is smaller or larger than intptr_t, truncate or extend
2608 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002609 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002610
2611 // If this is a multiply by a power of two, turn it into a shl
2612 // immediately. This is a very common case.
2613 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002614 if (ElementSize.isPowerOf2()) {
2615 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002616 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002617 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002618 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002619 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002620 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002621 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002622 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002623 }
2624 }
2625
Scott Michelfdc40a02009-02-17 22:15:04 +00002626 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002627 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002628 }
2629 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002630
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002631 setValue(&I, N);
2632}
2633
Dan Gohman46510a72010-04-15 01:51:59 +00002634void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002635 // If this is a fixed sized alloca in the entry block of the function,
2636 // allocate it statically on the stack.
2637 if (FuncInfo.StaticAllocaMap.count(&I))
2638 return; // getValue will auto-populate this.
2639
2640 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002641 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002642 unsigned Align =
2643 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2644 I.getAlignment());
2645
2646 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002647
Chris Lattner0b18e592009-03-17 19:36:00 +00002648 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2649 AllocSize,
2650 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002651
Owen Andersone50ed302009-08-10 22:56:29 +00002652 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002653 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002655 // Handle alignment. If the requested alignment is less than or equal to
2656 // the stack alignment, ignore it. If the size is greater than or equal to
2657 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002658 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659 if (Align <= StackAlign)
2660 Align = 0;
2661
2662 // Round the size of the allocation up to the stack alignment size
2663 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002664 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002665 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002666 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002668 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002669 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002670 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002671 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2672
2673 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002674 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002675 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002676 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002677 setValue(&I, DSA);
2678 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680 // Inform the Frame Information that we have just allocated a variable-sized
2681 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002682 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002683}
2684
Dan Gohman46510a72010-04-15 01:51:59 +00002685void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 const Value *SV = I.getOperand(0);
2687 SDValue Ptr = getValue(SV);
2688
2689 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002692 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002693 unsigned Alignment = I.getAlignment();
2694
Owen Andersone50ed302009-08-10 22:56:29 +00002695 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002696 SmallVector<uint64_t, 4> Offsets;
2697 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2698 unsigned NumValues = ValueVTs.size();
2699 if (NumValues == 0)
2700 return;
2701
2702 SDValue Root;
2703 bool ConstantMemory = false;
2704 if (I.isVolatile())
2705 // Serialize volatile loads with other side effects.
2706 Root = getRoot();
2707 else if (AA->pointsToConstantMemory(SV)) {
2708 // Do not serialize (non-volatile) loads of constant memory with anything.
2709 Root = DAG.getEntryNode();
2710 ConstantMemory = true;
2711 } else {
2712 // Do not serialize non-volatile loads against each other.
2713 Root = DAG.getRoot();
2714 }
2715
2716 SmallVector<SDValue, 4> Values(NumValues);
2717 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002718 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002719 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002720 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2721 PtrVT, Ptr,
2722 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002723 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002724 A, SV, Offsets[i], isVolatile,
2725 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002726
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 Values[i] = L;
2728 Chains[i] = L.getValue(1);
2729 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002730
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002731 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002732 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002733 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002734 if (isVolatile)
2735 DAG.setRoot(Chain);
2736 else
2737 PendingLoads.push_back(Chain);
2738 }
2739
Bill Wendling4533cac2010-01-28 21:51:40 +00002740 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2741 DAG.getVTList(&ValueVTs[0], NumValues),
2742 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002743}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002744
Dan Gohman46510a72010-04-15 01:51:59 +00002745void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2746 const Value *SrcV = I.getOperand(0);
2747 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748
Owen Andersone50ed302009-08-10 22:56:29 +00002749 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002750 SmallVector<uint64_t, 4> Offsets;
2751 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2752 unsigned NumValues = ValueVTs.size();
2753 if (NumValues == 0)
2754 return;
2755
2756 // Get the lowered operands. Note that we do this after
2757 // checking if NumResults is zero, because with zero results
2758 // the operands won't have values in the map.
2759 SDValue Src = getValue(SrcV);
2760 SDValue Ptr = getValue(PtrV);
2761
2762 SDValue Root = getRoot();
2763 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002764 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002766 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002768
2769 for (unsigned i = 0; i != NumValues; ++i) {
2770 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2771 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002772 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002773 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002774 Add, PtrV, Offsets[i], isVolatile,
2775 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002776 }
2777
Bill Wendling4533cac2010-01-28 21:51:40 +00002778 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2779 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002780}
2781
2782/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2783/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002784void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002785 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002786 bool HasChain = !I.doesNotAccessMemory();
2787 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2788
2789 // Build the operand list.
2790 SmallVector<SDValue, 8> Ops;
2791 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2792 if (OnlyLoad) {
2793 // We don't need to serialize loads against other loads.
2794 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002795 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 Ops.push_back(getRoot());
2797 }
2798 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002799
2800 // Info is set by getTgtMemInstrinsic
2801 TargetLowering::IntrinsicInfo Info;
2802 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2803
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002804 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002805 if (!IsTgtIntrinsic)
2806 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807
2808 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002809 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 SDValue Op = getValue(I.getOperand(i));
2811 assert(TLI.isTypeLegal(Op.getValueType()) &&
2812 "Intrinsic uses a non-legal type?");
2813 Ops.push_back(Op);
2814 }
2815
Owen Andersone50ed302009-08-10 22:56:29 +00002816 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002817 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2818#ifndef NDEBUG
2819 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2820 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2821 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002822 }
Bob Wilson8d919552009-07-31 22:41:21 +00002823#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002824
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002827
Bob Wilson8d919552009-07-31 22:41:21 +00002828 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829
2830 // Create the node.
2831 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002832 if (IsTgtIntrinsic) {
2833 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002834 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002835 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002836 Info.memVT, Info.ptrVal, Info.offset,
2837 Info.align, Info.vol,
2838 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002839 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002840 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002841 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002842 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002843 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002844 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002845 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002846 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002847 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002848 }
2849
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002850 if (HasChain) {
2851 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2852 if (OnlyLoad)
2853 PendingLoads.push_back(Chain);
2854 else
2855 DAG.setRoot(Chain);
2856 }
Bill Wendling856ff412009-12-22 00:12:37 +00002857
Benjamin Kramerf0127052010-01-05 13:12:22 +00002858 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002859 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002860 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002861 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002862 }
Bill Wendling856ff412009-12-22 00:12:37 +00002863
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864 setValue(&I, Result);
2865 }
2866}
2867
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002868/// GetSignificand - Get the significand and build it into a floating-point
2869/// number with exponent of 1:
2870///
2871/// Op = (Op & 0x007fffff) | 0x3f800000;
2872///
2873/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002874static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002875GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002876 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2877 DAG.getConstant(0x007fffff, MVT::i32));
2878 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2879 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002880 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002881}
2882
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002883/// GetExponent - Get the exponent:
2884///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002885/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002886///
2887/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002888static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002889GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002890 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2892 DAG.getConstant(0x7f800000, MVT::i32));
2893 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002894 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2896 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002897 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002898}
2899
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002900/// getF32Constant - Get 32-bit floating point constant.
2901static SDValue
2902getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002903 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002904}
2905
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002906/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002907/// visitIntrinsicCall: I is a call instruction
2908/// Op is the associated NodeType for I
2909const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002910SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2911 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002912 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002913 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002914 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002915 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002916 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002917 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002918 getValue(I.getOperand(2)),
2919 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920 setValue(&I, L);
2921 DAG.setRoot(L.getValue(1));
2922 return 0;
2923}
2924
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002925// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002926const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002927SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002928 SDValue Op1 = getValue(I.getOperand(1));
2929 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002930
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002932 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002933 return 0;
2934}
Bill Wendling74c37652008-12-09 22:08:41 +00002935
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002936/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2937/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002938void
Dan Gohman46510a72010-04-15 01:51:59 +00002939SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002940 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002941 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002942
Eric Christopher551754c2010-04-16 23:37:20 +00002943 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002944 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002945 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002946
2947 // Put the exponent in the right bit position for later addition to the
2948 // final result:
2949 //
2950 // #define LOG2OFe 1.4426950f
2951 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002953 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002955
2956 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2958 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002959
2960 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002961 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002962 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002963
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002964 if (LimitFloatPrecision <= 6) {
2965 // For floating-point precision of 6:
2966 //
2967 // TwoToFractionalPartOfX =
2968 // 0.997535578f +
2969 // (0.735607626f + 0.252464424f * x) * x;
2970 //
2971 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002972 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002973 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002975 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2977 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002978 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002980
2981 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002982 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002983 TwoToFracPartOfX, IntegerPartOfX);
2984
Owen Anderson825b72b2009-08-11 20:47:22 +00002985 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002986 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2987 // For floating-point precision of 12:
2988 //
2989 // TwoToFractionalPartOfX =
2990 // 0.999892986f +
2991 // (0.696457318f +
2992 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2993 //
2994 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002996 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002997 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002998 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3000 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003001 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003002 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3003 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003004 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003005 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003006
3007 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003009 TwoToFracPartOfX, IntegerPartOfX);
3010
Owen Anderson825b72b2009-08-11 20:47:22 +00003011 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003012 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3013 // For floating-point precision of 18:
3014 //
3015 // TwoToFractionalPartOfX =
3016 // 0.999999982f +
3017 // (0.693148872f +
3018 // (0.240227044f +
3019 // (0.554906021e-1f +
3020 // (0.961591928e-2f +
3021 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3022 //
3023 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003025 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003026 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003027 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003028 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3029 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003030 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003031 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3032 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003033 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003034 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3035 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003036 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003037 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3038 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003039 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3041 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003042 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003043 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003045
3046 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003048 TwoToFracPartOfX, IntegerPartOfX);
3049
Owen Anderson825b72b2009-08-11 20:47:22 +00003050 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003051 }
3052 } else {
3053 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003054 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003055 getValue(I.getOperand(1)).getValueType(),
3056 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003057 }
3058
Dale Johannesen59e577f2008-09-05 18:38:42 +00003059 setValue(&I, result);
3060}
3061
Bill Wendling39150252008-09-09 20:39:27 +00003062/// visitLog - Lower a log intrinsic. Handles the special sequences for
3063/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003064void
Dan Gohman46510a72010-04-15 01:51:59 +00003065SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003066 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003067 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003068
Eric Christopher551754c2010-04-16 23:37:20 +00003069 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003070 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003071 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003073
3074 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003075 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003077 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003078
3079 // Get the significand and build it into a floating-point number with
3080 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003081 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003082
3083 if (LimitFloatPrecision <= 6) {
3084 // For floating-point precision of 6:
3085 //
3086 // LogofMantissa =
3087 // -1.1609546f +
3088 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003089 //
Bill Wendling39150252008-09-09 20:39:27 +00003090 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003092 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003093 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003094 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3096 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003097 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003098
Scott Michelfdc40a02009-02-17 22:15:04 +00003099 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003100 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003101 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3102 // For floating-point precision of 12:
3103 //
3104 // LogOfMantissa =
3105 // -1.7417939f +
3106 // (2.8212026f +
3107 // (-1.4699568f +
3108 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3109 //
3110 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003112 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003113 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003114 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003115 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3116 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003117 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003118 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3119 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003120 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003121 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3122 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003123 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003124
Scott Michelfdc40a02009-02-17 22:15:04 +00003125 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003126 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003127 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3128 // For floating-point precision of 18:
3129 //
3130 // LogOfMantissa =
3131 // -2.1072184f +
3132 // (4.2372794f +
3133 // (-3.7029485f +
3134 // (2.2781945f +
3135 // (-0.87823314f +
3136 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3137 //
3138 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003140 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003141 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003142 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003143 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3144 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003145 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003146 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3147 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003148 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3150 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3153 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003154 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3156 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003157 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003158
Scott Michelfdc40a02009-02-17 22:15:04 +00003159 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003160 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003161 }
3162 } else {
3163 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003164 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003165 getValue(I.getOperand(1)).getValueType(),
3166 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003167 }
3168
Dale Johannesen59e577f2008-09-05 18:38:42 +00003169 setValue(&I, result);
3170}
3171
Bill Wendling3eb59402008-09-09 00:28:24 +00003172/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3173/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003174void
Dan Gohman46510a72010-04-15 01:51:59 +00003175SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003176 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003177 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003178
Eric Christopher551754c2010-04-16 23:37:20 +00003179 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003180 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003181 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003182 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003183
Bill Wendling39150252008-09-09 20:39:27 +00003184 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003185 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003186
Bill Wendling3eb59402008-09-09 00:28:24 +00003187 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003188 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003189 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003190
Bill Wendling3eb59402008-09-09 00:28:24 +00003191 // Different possible minimax approximations of significand in
3192 // floating-point for various degrees of accuracy over [1,2].
3193 if (LimitFloatPrecision <= 6) {
3194 // For floating-point precision of 6:
3195 //
3196 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3197 //
3198 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003201 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003202 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3204 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003205 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003206
Scott Michelfdc40a02009-02-17 22:15:04 +00003207 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003208 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003209 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3210 // For floating-point precision of 12:
3211 //
3212 // Log2ofMantissa =
3213 // -2.51285454f +
3214 // (4.07009056f +
3215 // (-2.12067489f +
3216 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003217 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003218 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003220 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003222 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003223 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3224 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3227 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003228 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3230 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003231 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003232
Scott Michelfdc40a02009-02-17 22:15:04 +00003233 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003235 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3236 // For floating-point precision of 18:
3237 //
3238 // Log2ofMantissa =
3239 // -3.0400495f +
3240 // (6.1129976f +
3241 // (-5.3420409f +
3242 // (3.2865683f +
3243 // (-1.2669343f +
3244 // (0.27515199f -
3245 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3246 //
3247 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003249 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003250 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003251 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3253 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003254 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003255 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3256 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003257 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3259 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003260 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003261 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3262 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003263 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003264 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3265 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003266 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003267
Scott Michelfdc40a02009-02-17 22:15:04 +00003268 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003269 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003270 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003271 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003272 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003273 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003274 getValue(I.getOperand(1)).getValueType(),
3275 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003276 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003277
Dale Johannesen59e577f2008-09-05 18:38:42 +00003278 setValue(&I, result);
3279}
3280
Bill Wendling3eb59402008-09-09 00:28:24 +00003281/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3282/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003283void
Dan Gohman46510a72010-04-15 01:51:59 +00003284SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003285 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003286 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003287
Eric Christopher551754c2010-04-16 23:37:20 +00003288 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003289 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003290 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003292
Bill Wendling39150252008-09-09 20:39:27 +00003293 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003294 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003296 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003297
3298 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003299 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003300 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003301
3302 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003303 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003304 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003305 // Log10ofMantissa =
3306 // -0.50419619f +
3307 // (0.60948995f - 0.10380950f * x) * x;
3308 //
3309 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003311 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003313 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3315 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003316 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003317
Scott Michelfdc40a02009-02-17 22:15:04 +00003318 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003320 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3321 // For floating-point precision of 12:
3322 //
3323 // Log10ofMantissa =
3324 // -0.64831180f +
3325 // (0.91751397f +
3326 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3327 //
3328 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003332 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3334 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003335 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3337 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003339
Scott Michelfdc40a02009-02-17 22:15:04 +00003340 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003342 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003343 // For floating-point precision of 18:
3344 //
3345 // Log10ofMantissa =
3346 // -0.84299375f +
3347 // (1.5327582f +
3348 // (-1.0688956f +
3349 // (0.49102474f +
3350 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3351 //
3352 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003354 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003356 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003357 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3358 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003359 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3361 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003362 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3364 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003365 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3367 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003368 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003369
Scott Michelfdc40a02009-02-17 22:15:04 +00003370 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003372 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003373 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003374 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003375 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003376 getValue(I.getOperand(1)).getValueType(),
3377 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003378 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003379
Dale Johannesen59e577f2008-09-05 18:38:42 +00003380 setValue(&I, result);
3381}
3382
Bill Wendlinge10c8142008-09-09 22:39:21 +00003383/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3384/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003385void
Dan Gohman46510a72010-04-15 01:51:59 +00003386SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003387 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003388 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003389
Eric Christopher551754c2010-04-16 23:37:20 +00003390 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003391 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003392 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003393
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003395
3396 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3398 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003399
3400 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003402 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003403
3404 if (LimitFloatPrecision <= 6) {
3405 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003406 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003407 // TwoToFractionalPartOfX =
3408 // 0.997535578f +
3409 // (0.735607626f + 0.252464424f * x) * x;
3410 //
3411 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003415 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003416 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3417 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003418 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003420 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003422
Scott Michelfdc40a02009-02-17 22:15:04 +00003423 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003425 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3426 // For floating-point precision of 12:
3427 //
3428 // TwoToFractionalPartOfX =
3429 // 0.999892986f +
3430 // (0.696457318f +
3431 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3432 //
3433 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003435 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003437 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003438 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3439 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003440 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3442 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003443 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003445 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003447
Scott Michelfdc40a02009-02-17 22:15:04 +00003448 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003450 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3451 // For floating-point precision of 18:
3452 //
3453 // TwoToFractionalPartOfX =
3454 // 0.999999982f +
3455 // (0.693148872f +
3456 // (0.240227044f +
3457 // (0.554906021e-1f +
3458 // (0.961591928e-2f +
3459 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3460 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003462 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3466 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003467 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3469 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003470 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3472 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003473 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003474 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3475 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003476 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003477 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3478 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003479 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003480 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003481 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003482 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003483
Scott Michelfdc40a02009-02-17 22:15:04 +00003484 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003485 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003486 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003487 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003488 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003489 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003490 getValue(I.getOperand(1)).getValueType(),
3491 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003492 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003493
Dale Johannesen601d3c02008-09-05 01:48:15 +00003494 setValue(&I, result);
3495}
3496
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003497/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3498/// limited-precision mode with x == 10.0f.
3499void
Dan Gohman46510a72010-04-15 01:51:59 +00003500SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003501 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003502 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003503 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003504 bool IsExp10 = false;
3505
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003507 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003508 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3509 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3510 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3511 APFloat Ten(10.0f);
3512 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3513 }
3514 }
3515 }
3516
3517 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003518 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003519
3520 // Put the exponent in the right bit position for later addition to the
3521 // final result:
3522 //
3523 // #define LOG2OF10 3.3219281f
3524 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003526 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003528
3529 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3531 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003532
3533 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003535 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003536
3537 if (LimitFloatPrecision <= 6) {
3538 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003539 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003540 // twoToFractionalPartOfX =
3541 // 0.997535578f +
3542 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003543 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003544 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003546 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003548 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3550 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003551 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003553 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003555
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003556 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003558 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3559 // For floating-point precision of 12:
3560 //
3561 // TwoToFractionalPartOfX =
3562 // 0.999892986f +
3563 // (0.696457318f +
3564 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3565 //
3566 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003570 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3572 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003573 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3575 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003576 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003578 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003580
Scott Michelfdc40a02009-02-17 22:15:04 +00003581 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003582 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003583 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3584 // For floating-point precision of 18:
3585 //
3586 // TwoToFractionalPartOfX =
3587 // 0.999999982f +
3588 // (0.693148872f +
3589 // (0.240227044f +
3590 // (0.554906021e-1f +
3591 // (0.961591928e-2f +
3592 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3593 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003595 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003597 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3599 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003600 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3602 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003603 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3605 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003606 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003607 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3608 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003609 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3611 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003612 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003614 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003616
Scott Michelfdc40a02009-02-17 22:15:04 +00003617 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003618 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003619 }
3620 } else {
3621 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003622 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003623 getValue(I.getOperand(1)).getValueType(),
3624 getValue(I.getOperand(1)),
3625 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003626 }
3627
3628 setValue(&I, result);
3629}
3630
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003631
3632/// ExpandPowI - Expand a llvm.powi intrinsic.
3633static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3634 SelectionDAG &DAG) {
3635 // If RHS is a constant, we can expand this out to a multiplication tree,
3636 // otherwise we end up lowering to a call to __powidf2 (for example). When
3637 // optimizing for size, we only want to do this if the expansion would produce
3638 // a small number of multiplies, otherwise we do the full expansion.
3639 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3640 // Get the exponent as a positive value.
3641 unsigned Val = RHSC->getSExtValue();
3642 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003643
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003644 // powi(x, 0) -> 1.0
3645 if (Val == 0)
3646 return DAG.getConstantFP(1.0, LHS.getValueType());
3647
Dan Gohmanae541aa2010-04-15 04:33:49 +00003648 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003649 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3650 // If optimizing for size, don't insert too many multiplies. This
3651 // inserts up to 5 multiplies.
3652 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3653 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003654 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003655 // powi(x,15) generates one more multiply than it should), but this has
3656 // the benefit of being both really simple and much better than a libcall.
3657 SDValue Res; // Logically starts equal to 1.0
3658 SDValue CurSquare = LHS;
3659 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003660 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003661 if (Res.getNode())
3662 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3663 else
3664 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003665 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003666
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003667 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3668 CurSquare, CurSquare);
3669 Val >>= 1;
3670 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003671
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003672 // If the original was negative, invert the result, producing 1/(x*x*x).
3673 if (RHSC->getSExtValue() < 0)
3674 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3675 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3676 return Res;
3677 }
3678 }
3679
3680 // Otherwise, expand to a libcall.
3681 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3682}
3683
3684
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003685/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3686/// we want to emit this as a call to a named external function, return the name
3687/// otherwise lower it and return null.
3688const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003689SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003690 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003691 SDValue Res;
3692
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003693 switch (Intrinsic) {
3694 default:
3695 // By default, turn this into a target intrinsic node.
3696 visitTargetIntrinsic(I, Intrinsic);
3697 return 0;
3698 case Intrinsic::vastart: visitVAStart(I); return 0;
3699 case Intrinsic::vaend: visitVAEnd(I); return 0;
3700 case Intrinsic::vacopy: visitVACopy(I); return 0;
3701 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003702 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003703 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003704 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003705 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003706 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003707 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003708 return 0;
3709 case Intrinsic::setjmp:
3710 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003711 case Intrinsic::longjmp:
3712 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003713 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003714 // Assert for address < 256 since we support only user defined address
3715 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003716 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003717 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003718 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003719 < 256 &&
3720 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003721 SDValue Op1 = getValue(I.getOperand(1));
3722 SDValue Op2 = getValue(I.getOperand(2));
3723 SDValue Op3 = getValue(I.getOperand(3));
3724 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3725 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003726 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003727 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003728 return 0;
3729 }
Chris Lattner824b9582008-11-21 16:42:48 +00003730 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003731 // Assert for address < 256 since we support only user defined address
3732 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003733 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003734 < 256 &&
3735 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003736 SDValue Op1 = getValue(I.getOperand(1));
3737 SDValue Op2 = getValue(I.getOperand(2));
3738 SDValue Op3 = getValue(I.getOperand(3));
3739 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3740 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003741 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003742 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003743 return 0;
3744 }
Chris Lattner824b9582008-11-21 16:42:48 +00003745 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003746 // Assert for address < 256 since we support only user defined address
3747 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003748 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003749 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003750 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003751 < 256 &&
3752 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003753 SDValue Op1 = getValue(I.getOperand(1));
3754 SDValue Op2 = getValue(I.getOperand(2));
3755 SDValue Op3 = getValue(I.getOperand(3));
3756 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3757 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003758
3759 // If the source and destination are known to not be aliases, we can
3760 // lower memmove as memcpy.
3761 uint64_t Size = -1ULL;
3762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003763 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003764 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003765 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003766 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003767 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003768 return 0;
3769 }
3770
Mon P Wang20adc9d2010-04-04 03:10:48 +00003771 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003772 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003773 return 0;
3774 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003775 case Intrinsic::dbg_declare: {
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003776 // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None.
3777 // The real handling of this intrinsic is in FastISel.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003778// if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00003779 // FIXME: Variable debug info is not supported here.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003780// return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003781 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003782 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003783 return 0;
3784
Devang Patelac1ceb32009-10-09 22:42:28 +00003785 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003786 // Parameters are handled specially.
3787 bool isParameter = false;
3788 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Variable->getOperand(0));
3789 if (CI) {
3790 unsigned Val = CI->getZExtValue();
3791 unsigned Tag = Val & ~LLVMDebugVersionMask;
3792 if (Tag == dwarf::DW_TAG_arg_variable)
3793 isParameter = true;
3794 }
Dan Gohman46510a72010-04-15 01:51:59 +00003795 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003796 if (!Address)
3797 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003798 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003799 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003800 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003801 if (AI) {
3802 // Don't handle byval arguments or VLAs, for example.
3803 // Non-byval arguments are handled here (they refer to the stack temporary
3804 // alloca at this point).
3805 DenseMap<const AllocaInst*, int>::iterator SI =
3806 FuncInfo.StaticAllocaMap.find(AI);
3807 if (SI == FuncInfo.StaticAllocaMap.end())
3808 return 0; // VLAs.
3809 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003810
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003811 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3812 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3813 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
3814 }
3815
3816 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3817 // but do not always have a corresponding SDNode built. The SDNodeOrder
3818 // absolute, but not relative, values are different depending on whether
3819 // debug info exists.
3820 ++SDNodeOrder;
3821 SDValue &N = NodeMap[Address];
3822 SDDbgValue *SDV;
3823 if (N.getNode()) {
3824 if (isParameter && !AI) {
3825 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
3826 if (FINode)
3827 // Byval parameter. We have a frame index at this point.
3828 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
3829 0, dl, SDNodeOrder);
3830 else
3831 // Can't do anything with other non-AI cases yet. This might be a
3832 // parameter of a callee function that got inlined, for example.
3833 return 0;
3834 } else if (AI)
3835 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
3836 0, dl, SDNodeOrder);
3837 else
3838 // Can't do anything with other non-AI cases yet.
3839 return 0;
3840 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
3841 } else {
3842 // This isn't useful, but it shows what we're missing.
3843 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
3844 0, dl, SDNodeOrder);
3845 DAG.AddDbgValue(SDV, 0, isParameter);
3846 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003847 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003848 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003849 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003850 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003851 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3852 return 0;
3853
3854 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003855 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003856 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003857 if (!V)
3858 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003859
3860 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3861 // but do not always have a corresponding SDNode built. The SDNodeOrder
3862 // absolute, but not relative, values are different depending on whether
3863 // debug info exists.
3864 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003865 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00003866 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003867 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
3868 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00003869 } else {
3870 SDValue &N = NodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003871 if (N.getNode()) {
3872 SDV = DAG.getDbgValue(Variable, N.getNode(),
3873 N.getResNo(), Offset, dl, SDNodeOrder);
3874 DAG.AddDbgValue(SDV, N.getNode(), false);
3875 } else {
Devang Patel00190342010-03-15 19:15:44 +00003876 // We may expand this to cover more cases. One case where we have no
3877 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003878 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
3879 Offset, dl, SDNodeOrder);
3880 DAG.AddDbgValue(SDV, 0, false);
3881 }
Devang Patel00190342010-03-15 19:15:44 +00003882 }
3883
3884 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003885 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003886 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003887 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003888 // Don't handle byval struct arguments or VLAs, for example.
3889 if (!AI)
3890 return 0;
3891 DenseMap<const AllocaInst*, int>::iterator SI =
3892 FuncInfo.StaticAllocaMap.find(AI);
3893 if (SI == FuncInfo.StaticAllocaMap.end())
3894 return 0; // VLAs.
3895 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003896
Chris Lattner512063d2010-04-05 06:19:28 +00003897 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3898 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3899 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003900 return 0;
3901 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003902 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003903 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003904 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3905 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003907 SDValue Ops[1];
3908 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003909 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003910 setValue(&I, Op);
3911 DAG.setRoot(Op.getValue(1));
3912 return 0;
3913 }
3914
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003915 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003916 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003917 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003918 if (CallMBB->isLandingPad())
3919 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003920 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003921#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003922 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003923#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003924 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3925 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003926 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003927 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003928
Chris Lattner3a5815f2009-09-17 23:54:54 +00003929 // Insert the EHSELECTION instruction.
3930 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3931 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003932 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003933 Ops[1] = getRoot();
3934 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003935 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003936 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003937 return 0;
3938 }
3939
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003940 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003941 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003942 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003943 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3944 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003945 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003946 return 0;
3947 }
3948
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003949 case Intrinsic::eh_return_i32:
3950 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003951 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3952 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3953 MVT::Other,
3954 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003955 getValue(I.getOperand(1)),
3956 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003957 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003958 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003959 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003960 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003961 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003962 EVT VT = getValue(I.getOperand(1)).getValueType();
3963 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003964 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003965 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003966 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003967 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003968 TLI.getPointerTy()),
3969 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003970 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003971 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003972 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00003973 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
3974 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003975 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003976 }
Jim Grosbachca752c92010-01-28 01:45:32 +00003977 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00003978 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00003979 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00003980 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00003981 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00003982
Chris Lattner512063d2010-04-05 06:19:28 +00003983 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00003984 return 0;
3985 }
3986
Mon P Wang77cdf302008-11-10 20:54:11 +00003987 case Intrinsic::convertff:
3988 case Intrinsic::convertfsi:
3989 case Intrinsic::convertfui:
3990 case Intrinsic::convertsif:
3991 case Intrinsic::convertuif:
3992 case Intrinsic::convertss:
3993 case Intrinsic::convertsu:
3994 case Intrinsic::convertus:
3995 case Intrinsic::convertuu: {
3996 ISD::CvtCode Code = ISD::CVT_INVALID;
3997 switch (Intrinsic) {
3998 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3999 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4000 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4001 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4002 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4003 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4004 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4005 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4006 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4007 }
Owen Andersone50ed302009-08-10 22:56:29 +00004008 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00004009 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004010 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4011 DAG.getValueType(DestVT),
4012 DAG.getValueType(getValue(Op1).getValueType()),
4013 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004014 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004015 Code);
4016 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004017 return 0;
4018 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004019 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004020 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004021 getValue(I.getOperand(1)).getValueType(),
4022 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004023 return 0;
4024 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00004025 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
4026 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004027 return 0;
4028 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004029 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004030 getValue(I.getOperand(1)).getValueType(),
4031 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004032 return 0;
4033 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004034 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004035 getValue(I.getOperand(1)).getValueType(),
4036 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004037 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004038 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004039 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004040 return 0;
4041 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004042 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004043 return 0;
4044 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004045 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004046 return 0;
4047 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004048 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004049 return 0;
4050 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004051 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004052 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004053 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004054 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004055 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004056 case Intrinsic::convert_to_fp16:
4057 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004058 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004059 return 0;
4060 case Intrinsic::convert_from_fp16:
4061 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004062 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004063 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004064 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004065 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004066 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004067 return 0;
4068 }
4069 case Intrinsic::readcyclecounter: {
4070 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004071 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4072 DAG.getVTList(MVT::i64, MVT::Other),
4073 &Op, 1);
4074 setValue(&I, Res);
4075 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004076 return 0;
4077 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004078 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004079 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004080 getValue(I.getOperand(1)).getValueType(),
4081 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004082 return 0;
4083 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004084 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004085 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004086 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004087 return 0;
4088 }
4089 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004090 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004091 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004092 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004093 return 0;
4094 }
4095 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004096 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004097 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004098 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004099 return 0;
4100 }
4101 case Intrinsic::stacksave: {
4102 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004103 Res = DAG.getNode(ISD::STACKSAVE, dl,
4104 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4105 setValue(&I, Res);
4106 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004107 return 0;
4108 }
4109 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004110 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004111 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004112 return 0;
4113 }
Bill Wendling57344502008-11-18 11:01:33 +00004114 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004115 // Emit code into the DAG to store the stack guard onto the stack.
4116 MachineFunction &MF = DAG.getMachineFunction();
4117 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004118 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004119
Eric Christopher551754c2010-04-16 23:37:20 +00004120 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4121 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004122
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004123 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004124 MFI->setStackProtectorIndex(FI);
4125
4126 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4127
4128 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004129 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4130 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004131 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004132 setValue(&I, Res);
4133 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004134 return 0;
4135 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004136 case Intrinsic::objectsize: {
4137 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004138 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004139
4140 assert(CI && "Non-constant type in __builtin_object_size?");
4141
Eric Christopher551754c2010-04-16 23:37:20 +00004142 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004143 EVT Ty = Arg.getValueType();
4144
Eric Christopherd060b252009-12-23 02:51:48 +00004145 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004146 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004147 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004148 Res = DAG.getConstant(0, Ty);
4149
4150 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004151 return 0;
4152 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004153 case Intrinsic::var_annotation:
4154 // Discard annotate attributes
4155 return 0;
4156
4157 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004158 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004159
4160 SDValue Ops[6];
4161 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004162 Ops[1] = getValue(I.getOperand(1));
4163 Ops[2] = getValue(I.getOperand(2));
4164 Ops[3] = getValue(I.getOperand(3));
4165 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004166 Ops[5] = DAG.getSrcValue(F);
4167
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004168 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4169 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4170 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004171
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004172 setValue(&I, Res);
4173 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004174 return 0;
4175 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004176 case Intrinsic::gcroot:
4177 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004178 const Value *Alloca = I.getOperand(1);
4179 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004180
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004181 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4182 GFI->addStackRoot(FI->getIndex(), TypeMap);
4183 }
4184 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004185 case Intrinsic::gcread:
4186 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004187 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004188 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004189 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004190 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004191 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004192 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004193 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004194 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004195 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004196 return implVisitAluOverflow(I, ISD::UADDO);
4197 case Intrinsic::sadd_with_overflow:
4198 return implVisitAluOverflow(I, ISD::SADDO);
4199 case Intrinsic::usub_with_overflow:
4200 return implVisitAluOverflow(I, ISD::USUBO);
4201 case Intrinsic::ssub_with_overflow:
4202 return implVisitAluOverflow(I, ISD::SSUBO);
4203 case Intrinsic::umul_with_overflow:
4204 return implVisitAluOverflow(I, ISD::UMULO);
4205 case Intrinsic::smul_with_overflow:
4206 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004207
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004208 case Intrinsic::prefetch: {
4209 SDValue Ops[4];
4210 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004211 Ops[1] = getValue(I.getOperand(1));
4212 Ops[2] = getValue(I.getOperand(2));
4213 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004214 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004215 return 0;
4216 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004218 case Intrinsic::memory_barrier: {
4219 SDValue Ops[6];
4220 Ops[0] = getRoot();
4221 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004222 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004223
Bill Wendling4533cac2010-01-28 21:51:40 +00004224 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004225 return 0;
4226 }
4227 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004228 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004229 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004230 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004231 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004232 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004233 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004234 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004235 getValue(I.getOperand(3)),
4236 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004237 setValue(&I, L);
4238 DAG.setRoot(L.getValue(1));
4239 return 0;
4240 }
4241 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004242 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004243 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004244 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004245 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004246 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004247 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004248 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004249 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004250 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004251 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004252 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004253 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004254 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004255 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004256 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004257 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004258 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004259 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004260 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004261 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004262 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004263
4264 case Intrinsic::invariant_start:
4265 case Intrinsic::lifetime_start:
4266 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004267 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004268 return 0;
4269 case Intrinsic::invariant_end:
4270 case Intrinsic::lifetime_end:
4271 // Discard region information.
4272 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004273 }
4274}
4275
Dan Gohman46510a72010-04-15 01:51:59 +00004276void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004277 bool isTailCall,
4278 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004279 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4280 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004281 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004282 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004283 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004284
4285 TargetLowering::ArgListTy Args;
4286 TargetLowering::ArgListEntry Entry;
4287 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004288
4289 // Check whether the function can return without sret-demotion.
4290 SmallVector<EVT, 4> OutVTs;
4291 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4292 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004293 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004294 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004295
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004296 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004297 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4298
4299 SDValue DemoteStackSlot;
4300
4301 if (!CanLowerReturn) {
4302 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4303 FTy->getReturnType());
4304 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4305 FTy->getReturnType());
4306 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004307 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004308 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4309
4310 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4311 Entry.Node = DemoteStackSlot;
4312 Entry.Ty = StackSlotPtrType;
4313 Entry.isSExt = false;
4314 Entry.isZExt = false;
4315 Entry.isInReg = false;
4316 Entry.isSRet = true;
4317 Entry.isNest = false;
4318 Entry.isByVal = false;
4319 Entry.Alignment = Align;
4320 Args.push_back(Entry);
4321 RetTy = Type::getVoidTy(FTy->getContext());
4322 }
4323
Dan Gohman46510a72010-04-15 01:51:59 +00004324 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004325 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 SDValue ArgNode = getValue(*i);
4327 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4328
4329 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004330 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4331 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4332 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4333 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4334 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4335 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004336 Entry.Alignment = CS.getParamAlignment(attrInd);
4337 Args.push_back(Entry);
4338 }
4339
Chris Lattner512063d2010-04-05 06:19:28 +00004340 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004341 // Insert a label before the invoke call to mark the try range. This can be
4342 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004343 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004344
Jim Grosbachca752c92010-01-28 01:45:32 +00004345 // For SjLj, keep track of which landing pads go with which invokes
4346 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004347 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004348 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004349 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004350 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004351 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004352 }
4353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004354 // Both PendingLoads and PendingExports must be flushed here;
4355 // this call might not return.
4356 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004357 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004358 }
4359
Dan Gohman98ca4f22009-08-05 01:29:28 +00004360 // Check if target-independent constraints permit a tail call here.
4361 // Target-dependent constraints are checked within TLI.LowerCallTo.
4362 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004363 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004364 isTailCall = false;
4365
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004367 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004368 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004369 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004370 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004371 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004372 isTailCall,
4373 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004374 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004375 assert((isTailCall || Result.second.getNode()) &&
4376 "Non-null chain expected with non-tail call!");
4377 assert((Result.second.getNode() || !Result.first.getNode()) &&
4378 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004379 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004381 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004382 // The instruction result is the result of loading from the
4383 // hidden sret parameter.
4384 SmallVector<EVT, 1> PVTs;
4385 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4386
4387 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4388 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4389 EVT PtrVT = PVTs[0];
4390 unsigned NumValues = OutVTs.size();
4391 SmallVector<SDValue, 4> Values(NumValues);
4392 SmallVector<SDValue, 4> Chains(NumValues);
4393
4394 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004395 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4396 DemoteStackSlot,
4397 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004398 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004399 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004400 Values[i] = L;
4401 Chains[i] = L.getValue(1);
4402 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004403
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004404 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4405 MVT::Other, &Chains[0], NumValues);
4406 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004407
4408 // Collect the legal value parts into potentially illegal values
4409 // that correspond to the original function's return values.
4410 SmallVector<EVT, 4> RetTys;
4411 RetTy = FTy->getReturnType();
4412 ComputeValueVTs(TLI, RetTy, RetTys);
4413 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4414 SmallVector<SDValue, 4> ReturnValues;
4415 unsigned CurReg = 0;
4416 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4417 EVT VT = RetTys[I];
4418 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4419 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4420
4421 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004422 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004423 RegisterVT, VT, AssertOp);
4424 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004425 CurReg += NumRegs;
4426 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004427
Bill Wendling4533cac2010-01-28 21:51:40 +00004428 setValue(CS.getInstruction(),
4429 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4430 DAG.getVTList(&RetTys[0], RetTys.size()),
4431 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004432
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004433 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004434
4435 // As a special case, a null chain means that a tail call has been emitted and
4436 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004437 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004438 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004439 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004440 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004441
Chris Lattner512063d2010-04-05 06:19:28 +00004442 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004443 // Insert a label at the end of the invoke call to mark the try range. This
4444 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004445 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004446 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004447
4448 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004449 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004450 }
4451}
4452
Chris Lattner8047d9a2009-12-24 00:37:38 +00004453/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4454/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004455static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4456 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004457 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004458 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004459 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004460 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004461 if (C->isNullValue())
4462 continue;
4463 // Unknown instruction.
4464 return false;
4465 }
4466 return true;
4467}
4468
Dan Gohman46510a72010-04-15 01:51:59 +00004469static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4470 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004471 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004472
Chris Lattner8047d9a2009-12-24 00:37:38 +00004473 // Check to see if this load can be trivially constant folded, e.g. if the
4474 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004475 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004476 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004477 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004478 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004479
Dan Gohman46510a72010-04-15 01:51:59 +00004480 if (const Constant *LoadCst =
4481 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4482 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004483 return Builder.getValue(LoadCst);
4484 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004485
Chris Lattner8047d9a2009-12-24 00:37:38 +00004486 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4487 // still constant memory, the input chain can be the entry node.
4488 SDValue Root;
4489 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004490
Chris Lattner8047d9a2009-12-24 00:37:38 +00004491 // Do not serialize (non-volatile) loads of constant memory with anything.
4492 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4493 Root = Builder.DAG.getEntryNode();
4494 ConstantMemory = true;
4495 } else {
4496 // Do not serialize non-volatile loads against each other.
4497 Root = Builder.DAG.getRoot();
4498 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004499
Chris Lattner8047d9a2009-12-24 00:37:38 +00004500 SDValue Ptr = Builder.getValue(PtrVal);
4501 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4502 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004503 false /*volatile*/,
4504 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004505
Chris Lattner8047d9a2009-12-24 00:37:38 +00004506 if (!ConstantMemory)
4507 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4508 return LoadVal;
4509}
4510
4511
4512/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4513/// If so, return true and lower it, otherwise return false and it will be
4514/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004515bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004516 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4517 if (I.getNumOperands() != 4)
4518 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004519
Eric Christopher551754c2010-04-16 23:37:20 +00004520 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004521 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004522 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004523 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004524 return false;
4525
Eric Christopher551754c2010-04-16 23:37:20 +00004526 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004527
Chris Lattner8047d9a2009-12-24 00:37:38 +00004528 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4529 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004530 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4531 bool ActuallyDoIt = true;
4532 MVT LoadVT;
4533 const Type *LoadTy;
4534 switch (Size->getZExtValue()) {
4535 default:
4536 LoadVT = MVT::Other;
4537 LoadTy = 0;
4538 ActuallyDoIt = false;
4539 break;
4540 case 2:
4541 LoadVT = MVT::i16;
4542 LoadTy = Type::getInt16Ty(Size->getContext());
4543 break;
4544 case 4:
4545 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004546 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004547 break;
4548 case 8:
4549 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004550 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004551 break;
4552 /*
4553 case 16:
4554 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004555 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004556 LoadTy = VectorType::get(LoadTy, 4);
4557 break;
4558 */
4559 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004560
Chris Lattner04b091a2009-12-24 01:07:17 +00004561 // This turns into unaligned loads. We only do this if the target natively
4562 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4563 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004564
Chris Lattner04b091a2009-12-24 01:07:17 +00004565 // Require that we can find a legal MVT, and only do this if the target
4566 // supports unaligned loads of that type. Expanding into byte loads would
4567 // bloat the code.
4568 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4569 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4570 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4571 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4572 ActuallyDoIt = false;
4573 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004574
Chris Lattner04b091a2009-12-24 01:07:17 +00004575 if (ActuallyDoIt) {
4576 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4577 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004578
Chris Lattner04b091a2009-12-24 01:07:17 +00004579 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4580 ISD::SETNE);
4581 EVT CallVT = TLI.getValueType(I.getType(), true);
4582 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4583 return true;
4584 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004585 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004586
4587
Chris Lattner8047d9a2009-12-24 00:37:38 +00004588 return false;
4589}
4590
4591
Dan Gohman46510a72010-04-15 01:51:59 +00004592void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004593 const char *RenameFn = 0;
4594 if (Function *F = I.getCalledFunction()) {
4595 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004596 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004597 if (II) {
4598 if (unsigned IID = II->getIntrinsicID(F)) {
4599 RenameFn = visitIntrinsicCall(I, IID);
4600 if (!RenameFn)
4601 return;
4602 }
4603 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604 if (unsigned IID = F->getIntrinsicID()) {
4605 RenameFn = visitIntrinsicCall(I, IID);
4606 if (!RenameFn)
4607 return;
4608 }
4609 }
4610
4611 // Check for well-known libc/libm calls. If the function is internal, it
4612 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004613 if (!F->hasLocalLinkage() && F->hasName()) {
4614 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004615 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004616 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004617 I.getOperand(1)->getType()->isFloatingPointTy() &&
4618 I.getType() == I.getOperand(1)->getType() &&
4619 I.getType() == I.getOperand(2)->getType()) {
4620 SDValue LHS = getValue(I.getOperand(1));
4621 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004622 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4623 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 return;
4625 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004626 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004627 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004628 I.getOperand(1)->getType()->isFloatingPointTy() &&
4629 I.getType() == I.getOperand(1)->getType()) {
4630 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004631 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4632 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004633 return;
4634 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004635 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004636 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004637 I.getOperand(1)->getType()->isFloatingPointTy() &&
4638 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004639 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004640 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004641 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4642 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004643 return;
4644 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004645 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004646 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004647 I.getOperand(1)->getType()->isFloatingPointTy() &&
4648 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004649 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004650 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004651 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4652 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004653 return;
4654 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004655 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4656 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004657 I.getOperand(1)->getType()->isFloatingPointTy() &&
4658 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004659 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004660 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004661 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4662 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004663 return;
4664 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004665 } else if (Name == "memcmp") {
4666 if (visitMemCmpCall(I))
4667 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 }
4669 }
Eric Christopher551754c2010-04-16 23:37:20 +00004670 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671 visitInlineAsm(&I);
4672 return;
4673 }
4674
4675 SDValue Callee;
4676 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004677 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004678 else
Bill Wendling056292f2008-09-16 21:48:12 +00004679 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004680
Bill Wendling0d580132009-12-23 01:28:19 +00004681 // Check if we can potentially perform a tail call. More detailed checking is
4682 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004683 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684}
4685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004686/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004687/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688/// Chain/Flag as the input and updates them for the output Chain/Flag.
4689/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004690SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004691 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004692 // Assemble the legal parts into the final values.
4693 SmallVector<SDValue, 4> Values(ValueVTs.size());
4694 SmallVector<SDValue, 8> Parts;
4695 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4696 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004697 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004698 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004699 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004700
4701 Parts.resize(NumRegs);
4702 for (unsigned i = 0; i != NumRegs; ++i) {
4703 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004704 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004705 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004706 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004707 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004708 *Flag = P.getValue(2);
4709 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004710
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004711 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004712
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004713 // If the source register was virtual and if we know something about it,
4714 // add an assert node.
4715 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4716 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4717 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4718 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4719 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4720 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004721
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004722 unsigned RegSize = RegisterVT.getSizeInBits();
4723 unsigned NumSignBits = LOI.NumSignBits;
4724 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004726 // FIXME: We capture more information than the dag can represent. For
4727 // now, just use the tightest assertzext/assertsext possible.
4728 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004729 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004730 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004731 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004732 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004733 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004734 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004735 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004736 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004737 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004738 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004740 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004741 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004742 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004744 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004746
Bill Wendling4533cac2010-01-28 21:51:40 +00004747 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004748 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004749 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004750 }
4751 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004752
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004753 Parts[i] = P;
4754 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004755
Bill Wendling46ada192010-03-02 01:55:18 +00004756 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004757 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004758 Part += NumRegs;
4759 Parts.clear();
4760 }
4761
Bill Wendling4533cac2010-01-28 21:51:40 +00004762 return DAG.getNode(ISD::MERGE_VALUES, dl,
4763 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4764 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004765}
4766
4767/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004768/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769/// Chain/Flag as the input and updates them for the output Chain/Flag.
4770/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004771void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004772 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004773 // Get the list of the values's legal parts.
4774 unsigned NumRegs = Regs.size();
4775 SmallVector<SDValue, 8> Parts(NumRegs);
4776 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004777 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004778 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004779 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004780
Bill Wendling46ada192010-03-02 01:55:18 +00004781 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004782 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004783 &Parts[Part], NumParts, RegisterVT);
4784 Part += NumParts;
4785 }
4786
4787 // Copy the parts into the registers.
4788 SmallVector<SDValue, 8> Chains(NumRegs);
4789 for (unsigned i = 0; i != NumRegs; ++i) {
4790 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004791 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004792 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004793 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004794 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004795 *Flag = Part.getValue(1);
4796 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004797
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 Chains[i] = Part.getValue(0);
4799 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004800
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004801 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004802 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004803 // flagged to it. That is the CopyToReg nodes and the user are considered
4804 // a single scheduling unit. If we create a TokenFactor and return it as
4805 // chain, then the TokenFactor is both a predecessor (operand) of the
4806 // user as well as a successor (the TF operands are flagged to the user).
4807 // c1, f1 = CopyToReg
4808 // c2, f2 = CopyToReg
4809 // c3 = TokenFactor c1, c2
4810 // ...
4811 // = op c3, ..., f2
4812 Chain = Chains[NumRegs-1];
4813 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004814 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004815}
4816
4817/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004818/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004819/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004820void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4821 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004822 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004823 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004824 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004825 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004826 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004827 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004828 Ops.push_back(Res);
4829
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004830 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004831 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004832 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004833 for (unsigned i = 0; i != NumRegs; ++i) {
4834 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004835 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004836 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837 }
4838}
4839
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004840/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004841/// i.e. it isn't a stack pointer or some other special register, return the
4842/// register class for the register. Otherwise, return null.
4843static const TargetRegisterClass *
4844isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4845 const TargetLowering &TLI,
4846 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004848 const TargetRegisterClass *FoundRC = 0;
4849 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4850 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004852
4853 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004854 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004855 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4856 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4857 I != E; ++I) {
4858 if (TLI.isTypeLegal(*I)) {
4859 // If we have already found this register in a different register class,
4860 // choose the one with the largest VT specified. For example, on
4861 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004863 ThisVT = *I;
4864 break;
4865 }
4866 }
4867 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004868
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004870
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004871 // NOTE: This isn't ideal. In particular, this might allocate the
4872 // frame pointer in functions that need it (due to them not being taken
4873 // out of allocation, because a variable sized allocation hasn't been seen
4874 // yet). This is a slight code pessimization, but should still work.
4875 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4876 E = RC->allocation_order_end(MF); I != E; ++I)
4877 if (*I == Reg) {
4878 // We found a matching register class. Keep looking at others in case
4879 // we find one with larger registers that this physreg is also in.
4880 FoundRC = RC;
4881 FoundVT = ThisVT;
4882 break;
4883 }
4884 }
4885 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004886}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004887
4888
4889namespace llvm {
4890/// AsmOperandInfo - This contains information for each constraint that we are
4891/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004892class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004893 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004894public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004895 /// CallOperand - If this is the result output operand or a clobber
4896 /// this is null, otherwise it is the incoming operand to the CallInst.
4897 /// This gets modified as the asm is processed.
4898 SDValue CallOperand;
4899
4900 /// AssignedRegs - If this is a register or register class operand, this
4901 /// contains the set of register corresponding to the operand.
4902 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004903
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004904 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4905 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4906 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004908 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4909 /// busy in OutputRegs/InputRegs.
4910 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004911 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004912 std::set<unsigned> &InputRegs,
4913 const TargetRegisterInfo &TRI) const {
4914 if (isOutReg) {
4915 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4916 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4917 }
4918 if (isInReg) {
4919 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4920 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4921 }
4922 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004923
Owen Andersone50ed302009-08-10 22:56:29 +00004924 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004925 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004926 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004927 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004928 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004929 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004930 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004931
Chris Lattner81249c92008-10-17 17:05:25 +00004932 if (isa<BasicBlock>(CallOperandVal))
4933 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004934
Chris Lattner81249c92008-10-17 17:05:25 +00004935 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004936
Chris Lattner81249c92008-10-17 17:05:25 +00004937 // If this is an indirect operand, the operand is a pointer to the
4938 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004939 if (isIndirect) {
4940 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4941 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004942 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004943 OpTy = PtrTy->getElementType();
4944 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004945
Chris Lattner81249c92008-10-17 17:05:25 +00004946 // If OpTy is not a single value, it may be a struct/union that we
4947 // can tile with integers.
4948 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4949 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4950 switch (BitSize) {
4951 default: break;
4952 case 1:
4953 case 8:
4954 case 16:
4955 case 32:
4956 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004957 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004958 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004959 break;
4960 }
4961 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004962
Chris Lattner81249c92008-10-17 17:05:25 +00004963 return TLI.getValueType(OpTy, true);
4964 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004966private:
4967 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4968 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004969 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004970 const TargetRegisterInfo &TRI) {
4971 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4972 Regs.insert(Reg);
4973 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4974 for (; *Aliases; ++Aliases)
4975 Regs.insert(*Aliases);
4976 }
4977};
4978} // end llvm namespace.
4979
4980
4981/// GetRegistersForValue - Assign registers (virtual or physical) for the
4982/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004983/// register allocator to handle the assignment process. However, if the asm
4984/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004985/// allocation. This produces generally horrible, but correct, code.
4986///
4987/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004988/// Input and OutputRegs are the set of already allocated physical registers.
4989///
Dan Gohman2048b852009-11-23 18:04:58 +00004990void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004991GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004992 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004993 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004994 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004995
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004996 // Compute whether this value requires an input register, an output register,
4997 // or both.
4998 bool isOutReg = false;
4999 bool isInReg = false;
5000 switch (OpInfo.Type) {
5001 case InlineAsm::isOutput:
5002 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005003
5004 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005005 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005006 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005007 break;
5008 case InlineAsm::isInput:
5009 isInReg = true;
5010 isOutReg = false;
5011 break;
5012 case InlineAsm::isClobber:
5013 isOutReg = true;
5014 isInReg = true;
5015 break;
5016 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005017
5018
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005019 MachineFunction &MF = DAG.getMachineFunction();
5020 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005021
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005022 // If this is a constraint for a single physreg, or a constraint for a
5023 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005024 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005025 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5026 OpInfo.ConstraintVT);
5027
5028 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005029 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005030 // If this is a FP input in an integer register (or visa versa) insert a bit
5031 // cast of the input value. More generally, handle any case where the input
5032 // value disagrees with the register class we plan to stick this in.
5033 if (OpInfo.Type == InlineAsm::isInput &&
5034 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005035 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005036 // types are identical size, use a bitcast to convert (e.g. two differing
5037 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005038 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005039 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005040 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005041 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005042 OpInfo.ConstraintVT = RegVT;
5043 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5044 // If the input is a FP value and we want it in FP registers, do a
5045 // bitcast to the corresponding integer type. This turns an f64 value
5046 // into i64, which can be passed with two i32 values on a 32-bit
5047 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005048 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005049 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005050 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005051 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005052 OpInfo.ConstraintVT = RegVT;
5053 }
5054 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005055
Owen Anderson23b9b192009-08-12 00:36:31 +00005056 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005057 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005058
Owen Andersone50ed302009-08-10 22:56:29 +00005059 EVT RegVT;
5060 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061
5062 // If this is a constraint for a specific physical register, like {r17},
5063 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005064 if (unsigned AssignedReg = PhysReg.first) {
5065 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005066 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005067 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 // Get the actual register value type. This is important, because the user
5070 // may have asked for (e.g.) the AX register in i32 type. We need to
5071 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005072 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005074 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005075 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005076
5077 // If this is an expanded reference, add the rest of the regs to Regs.
5078 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005079 TargetRegisterClass::iterator I = RC->begin();
5080 for (; *I != AssignedReg; ++I)
5081 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005083 // Already added the first reg.
5084 --NumRegs; ++I;
5085 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005086 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005087 Regs.push_back(*I);
5088 }
5089 }
Bill Wendling651ad132009-12-22 01:25:10 +00005090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5092 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5093 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5094 return;
5095 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005097 // Otherwise, if this was a reference to an LLVM register class, create vregs
5098 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005099 if (const TargetRegisterClass *RC = PhysReg.second) {
5100 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005101 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005102 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005103
Evan Chengfb112882009-03-23 08:01:15 +00005104 // Create the appropriate number of virtual registers.
5105 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5106 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005107 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005108
Evan Chengfb112882009-03-23 08:01:15 +00005109 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5110 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005112
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005113 // This is a reference to a register class that doesn't directly correspond
5114 // to an LLVM register class. Allocate NumRegs consecutive, available,
5115 // registers from the class.
5116 std::vector<unsigned> RegClassRegs
5117 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5118 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005120 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5121 unsigned NumAllocated = 0;
5122 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5123 unsigned Reg = RegClassRegs[i];
5124 // See if this register is available.
5125 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5126 (isInReg && InputRegs.count(Reg))) { // Already used.
5127 // Make sure we find consecutive registers.
5128 NumAllocated = 0;
5129 continue;
5130 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005131
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005132 // Check to see if this register is allocatable (i.e. don't give out the
5133 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005134 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5135 if (!RC) { // Couldn't allocate this register.
5136 // Reset NumAllocated to make sure we return consecutive registers.
5137 NumAllocated = 0;
5138 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005139 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005141 // Okay, this register is good, we can use it.
5142 ++NumAllocated;
5143
5144 // If we allocated enough consecutive registers, succeed.
5145 if (NumAllocated == NumRegs) {
5146 unsigned RegStart = (i-NumAllocated)+1;
5147 unsigned RegEnd = i+1;
5148 // Mark all of the allocated registers used.
5149 for (unsigned i = RegStart; i != RegEnd; ++i)
5150 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005151
5152 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153 OpInfo.ConstraintVT);
5154 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5155 return;
5156 }
5157 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005158
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159 // Otherwise, we couldn't allocate enough registers for this.
5160}
5161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005162/// visitInlineAsm - Handle a call to an InlineAsm object.
5163///
Dan Gohman46510a72010-04-15 01:51:59 +00005164void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5165 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005166
5167 /// ConstraintOperands - Information about all of the constraints.
5168 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170 std::set<unsigned> OutputRegs, InputRegs;
5171
5172 // Do a prepass over the constraints, canonicalizing them, and building up the
5173 // ConstraintOperands list.
5174 std::vector<InlineAsm::ConstraintInfo>
5175 ConstraintInfos = IA->ParseConstraints();
5176
Evan Chengda43bcf2008-09-24 00:05:32 +00005177 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005178
Chris Lattner6c147292009-04-30 00:48:50 +00005179 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005180
Chris Lattner6c147292009-04-30 00:48:50 +00005181 // We won't need to flush pending loads if this asm doesn't touch
5182 // memory and is nonvolatile.
5183 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005184 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005185 else
5186 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005188 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5189 unsigned ResNo = 0; // ResNo - The result number of the next output.
5190 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5191 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5192 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005193
Owen Anderson825b72b2009-08-11 20:47:22 +00005194 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005195
5196 // Compute the value type for each operand.
5197 switch (OpInfo.Type) {
5198 case InlineAsm::isOutput:
5199 // Indirect outputs just consume an argument.
5200 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005201 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005202 break;
5203 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005205 // The return value of the call is this value. As such, there is no
5206 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005207 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005208 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005209 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5210 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5211 } else {
5212 assert(ResNo == 0 && "Asm only has one result!");
5213 OpVT = TLI.getValueType(CS.getType());
5214 }
5215 ++ResNo;
5216 break;
5217 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005218 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005219 break;
5220 case InlineAsm::isClobber:
5221 // Nothing to do.
5222 break;
5223 }
5224
5225 // If this is an input or an indirect output, process the call argument.
5226 // BasicBlocks are labels, currently appearing only in asm's.
5227 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005228 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005229 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5230
Dan Gohman46510a72010-04-15 01:51:59 +00005231 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005232 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005233 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005234 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005235 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005236
Owen Anderson1d0be152009-08-13 21:58:54 +00005237 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005241 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005242
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005243 // Second pass over the constraints: compute which constraint option to use
5244 // and assign registers to constraints that want a specific physreg.
5245 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5246 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005247
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005248 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005249 // matching input. If their types mismatch, e.g. one is an integer, the
5250 // other is floating point, or their sizes are different, flag it as an
5251 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005252 if (OpInfo.hasMatchingInput()) {
5253 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005254
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005255 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005256 if ((OpInfo.ConstraintVT.isInteger() !=
5257 Input.ConstraintVT.isInteger()) ||
5258 (OpInfo.ConstraintVT.getSizeInBits() !=
5259 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005260 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005261 " with a matching output constraint of"
5262 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005263 }
5264 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005265 }
5266 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005269 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005270
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005271 // If this is a memory input, and if the operand is not indirect, do what we
5272 // need to to provide an address for the memory input.
5273 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5274 !OpInfo.isIndirect) {
5275 assert(OpInfo.Type == InlineAsm::isInput &&
5276 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278 // Memory operands really want the address of the value. If we don't have
5279 // an indirect input, put it in the constpool if we can, otherwise spill
5280 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282 // If the operand is a float, integer, or vector constant, spill to a
5283 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005284 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005285 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5286 isa<ConstantVector>(OpVal)) {
5287 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5288 TLI.getPointerTy());
5289 } else {
5290 // Otherwise, create a stack slot and emit a store to it before the
5291 // asm.
5292 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005293 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005294 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5295 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005296 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005297 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005298 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005299 OpInfo.CallOperand, StackSlot, NULL, 0,
5300 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301 OpInfo.CallOperand = StackSlot;
5302 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005304 // There is no longer a Value* corresponding to this operand.
5305 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 // It is now an indirect operand.
5308 OpInfo.isIndirect = true;
5309 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005311 // If this constraint is for a specific register, allocate it before
5312 // anything else.
5313 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005314 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005315 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005316
Bill Wendling651ad132009-12-22 01:25:10 +00005317 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005320 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005321 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5322 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005323
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005324 // C_Register operands have already been allocated, Other/Memory don't need
5325 // to be.
5326 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005327 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005328 }
5329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005330 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5331 std::vector<SDValue> AsmNodeOperands;
5332 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5333 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005334 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5335 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005336
Chris Lattnerdecc2672010-04-07 05:20:54 +00005337 // If we have a !srcloc metadata node associated with it, we want to attach
5338 // this to the ultimately generated inline asm machineinstr. To do this, we
5339 // pass in the third operand as this (potentially null) inline asm MDNode.
5340 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5341 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005342
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005343 // Loop over all of the inputs, copying the operand values into the
5344 // appropriate registers and processing the output regs.
5345 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005346
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005347 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5348 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005349
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005350 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5351 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5352
5353 switch (OpInfo.Type) {
5354 case InlineAsm::isOutput: {
5355 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5356 OpInfo.ConstraintType != TargetLowering::C_Register) {
5357 // Memory output, or 'other' output (e.g. 'X' constraint).
5358 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5359
5360 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005361 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5362 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005363 TLI.getPointerTy()));
5364 AsmNodeOperands.push_back(OpInfo.CallOperand);
5365 break;
5366 }
5367
5368 // Otherwise, this is a register or register class output.
5369
5370 // Copy the output from the appropriate register. Find a register that
5371 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005372 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005373 report_fatal_error("Couldn't allocate output reg for constraint '" +
5374 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375
5376 // If this is an indirect operand, store through the pointer after the
5377 // asm.
5378 if (OpInfo.isIndirect) {
5379 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5380 OpInfo.CallOperandVal));
5381 } else {
5382 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005383 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384 // Concatenate this output onto the outputs list.
5385 RetValRegs.append(OpInfo.AssignedRegs);
5386 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005387
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388 // Add information to the INLINEASM node to know that this register is
5389 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005390 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005391 InlineAsm::Kind_RegDefEarlyClobber :
5392 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005393 false,
5394 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005395 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005396 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397 break;
5398 }
5399 case InlineAsm::isInput: {
5400 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005401
Chris Lattner6bdcda32008-10-17 16:47:46 +00005402 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 // If this is required to match an output register we have already set,
5404 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005405 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005407 // Scan until we find the definition we already emitted of this operand.
5408 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005409 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410 for (; OperandNo; --OperandNo) {
5411 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005412 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005413 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005414 assert((InlineAsm::isRegDefKind(OpFlag) ||
5415 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5416 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005417 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418 }
5419
Evan Cheng697cbbf2009-03-20 18:03:34 +00005420 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005421 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005422 if (InlineAsm::isRegDefKind(OpFlag) ||
5423 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005424 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005425 if (OpInfo.isIndirect) {
5426 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005427 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005428 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5429 " don't know how to handle tied "
5430 "indirect register inputs");
5431 }
5432
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005433 RegsForValue MatchedRegs;
5434 MatchedRegs.TLI = &TLI;
5435 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005436 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005437 MatchedRegs.RegVTs.push_back(RegVT);
5438 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005439 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005440 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005441 MatchedRegs.Regs.push_back
5442 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005443
5444 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005445 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005446 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005447 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005448 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005449 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005450 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005451 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005452
5453 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5454 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5455 "Unexpected number of operands");
5456 // Add information to the INLINEASM node to know about this input.
5457 // See InlineAsm.h isUseOperandTiedToDef.
5458 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5459 OpInfo.getMatchedOperand());
5460 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5461 TLI.getPointerTy()));
5462 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5463 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005465
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005466 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005467 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005470 std::vector<SDValue> Ops;
5471 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005472 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005473 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005474 report_fatal_error("Invalid operand for inline asm constraint '" +
5475 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005476
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005477 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005478 unsigned ResOpType =
5479 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005480 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005481 TLI.getPointerTy()));
5482 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5483 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005484 }
5485
5486 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005487 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5488 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5489 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005491 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005492 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005493 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494 TLI.getPointerTy()));
5495 AsmNodeOperands.push_back(InOperandVal);
5496 break;
5497 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005498
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005499 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5500 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5501 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005502 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005503 "Don't know how to handle indirect register inputs yet!");
5504
5505 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005506 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005507 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005508 report_fatal_error("Couldn't allocate input reg for constraint '" +
5509 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005510
Dale Johannesen66978ee2009-01-31 02:22:37 +00005511 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005512 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005513
Chris Lattnerdecc2672010-04-07 05:20:54 +00005514 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005515 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 break;
5517 }
5518 case InlineAsm::isClobber: {
5519 // Add the clobbered value to the operand list, so that the register
5520 // allocator is aware that the physreg got clobbered.
5521 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005522 OpInfo.AssignedRegs.AddInlineAsmOperands(
5523 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005524 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005525 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005526 break;
5527 }
5528 }
5529 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005530
Chris Lattnerdecc2672010-04-07 05:20:54 +00005531 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005532 AsmNodeOperands[0] = Chain;
5533 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005534
Dale Johannesen66978ee2009-01-31 02:22:37 +00005535 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005537 &AsmNodeOperands[0], AsmNodeOperands.size());
5538 Flag = Chain.getValue(1);
5539
5540 // If this asm returns a register value, copy the result from that register
5541 // and set it as the value of the call.
5542 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005543 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005544 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005545
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005546 // FIXME: Why don't we do this for inline asms with MRVs?
5547 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005548 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005549
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005550 // If any of the results of the inline asm is a vector, it may have the
5551 // wrong width/num elts. This can happen for register classes that can
5552 // contain multiple different value types. The preg or vreg allocated may
5553 // not have the same VT as was expected. Convert it to the right type
5554 // with bit_convert.
5555 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005556 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005557 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005558
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005559 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005560 ResultType.isInteger() && Val.getValueType().isInteger()) {
5561 // If a result value was tied to an input value, the computed result may
5562 // have a wider width than the expected result. Extract the relevant
5563 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005564 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005565 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005566
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005567 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005568 }
Dan Gohman95915732008-10-18 01:03:45 +00005569
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005570 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005571 // Don't need to use this as a chain in this case.
5572 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5573 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005574 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005575
Dan Gohman46510a72010-04-15 01:51:59 +00005576 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005577
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005578 // Process indirect outputs, first output all of the flagged copies out of
5579 // physregs.
5580 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5581 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005582 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005583 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005584 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005585 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5586 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005587
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005588 // Emit the non-flagged stores from the physregs.
5589 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005590 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5591 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5592 StoresToEmit[i].first,
5593 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005594 StoresToEmit[i].second, 0,
5595 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005596 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005597 }
5598
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005599 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005600 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005601 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005602
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 DAG.setRoot(Chain);
5604}
5605
Dan Gohman46510a72010-04-15 01:51:59 +00005606void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005607 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5608 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005609 getValue(I.getOperand(1)),
5610 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005611}
5612
Dan Gohman46510a72010-04-15 01:51:59 +00005613void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005614 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5615 getRoot(), getValue(I.getOperand(0)),
5616 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005617 setValue(&I, V);
5618 DAG.setRoot(V.getValue(1));
5619}
5620
Dan Gohman46510a72010-04-15 01:51:59 +00005621void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005622 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5623 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005624 getValue(I.getOperand(1)),
5625 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626}
5627
Dan Gohman46510a72010-04-15 01:51:59 +00005628void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005629 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5630 MVT::Other, getRoot(),
5631 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005632 getValue(I.getOperand(2)),
5633 DAG.getSrcValue(I.getOperand(1)),
5634 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005635}
5636
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005637/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005638/// implementation, which just calls LowerCall.
5639/// FIXME: When all targets are
5640/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005641std::pair<SDValue, SDValue>
5642TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5643 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005644 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005645 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005646 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005647 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005648 ArgListTy &Args, SelectionDAG &DAG,
5649 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005650 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005651 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005653 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5655 for (unsigned Value = 0, NumValues = ValueVTs.size();
5656 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005657 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005658 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005659 SDValue Op = SDValue(Args[i].Node.getNode(),
5660 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661 ISD::ArgFlagsTy Flags;
5662 unsigned OriginalAlignment =
5663 getTargetData()->getABITypeAlignment(ArgTy);
5664
5665 if (Args[i].isZExt)
5666 Flags.setZExt();
5667 if (Args[i].isSExt)
5668 Flags.setSExt();
5669 if (Args[i].isInReg)
5670 Flags.setInReg();
5671 if (Args[i].isSRet)
5672 Flags.setSRet();
5673 if (Args[i].isByVal) {
5674 Flags.setByVal();
5675 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5676 const Type *ElementTy = Ty->getElementType();
5677 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005678 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005679 // For ByVal, alignment should come from FE. BE will guess if this
5680 // info is not there but there are cases it cannot get right.
5681 if (Args[i].Alignment)
5682 FrameAlign = Args[i].Alignment;
5683 Flags.setByValAlign(FrameAlign);
5684 Flags.setByValSize(FrameSize);
5685 }
5686 if (Args[i].isNest)
5687 Flags.setNest();
5688 Flags.setOrigAlign(OriginalAlignment);
5689
Owen Anderson23b9b192009-08-12 00:36:31 +00005690 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5691 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005692 SmallVector<SDValue, 4> Parts(NumParts);
5693 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5694
5695 if (Args[i].isSExt)
5696 ExtendKind = ISD::SIGN_EXTEND;
5697 else if (Args[i].isZExt)
5698 ExtendKind = ISD::ZERO_EXTEND;
5699
Bill Wendling46ada192010-03-02 01:55:18 +00005700 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005701 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702
Dan Gohman98ca4f22009-08-05 01:29:28 +00005703 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005704 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005705 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5706 if (NumParts > 1 && j == 0)
5707 MyFlags.Flags.setSplit();
5708 else if (j != 0)
5709 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005710
Dan Gohman98ca4f22009-08-05 01:29:28 +00005711 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005712 }
5713 }
5714 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005715
Dan Gohman98ca4f22009-08-05 01:29:28 +00005716 // Handle the incoming return values from the call.
5717 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005718 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005719 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005720 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005721 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005722 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5723 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005724 for (unsigned i = 0; i != NumRegs; ++i) {
5725 ISD::InputArg MyFlags;
5726 MyFlags.VT = RegisterVT;
5727 MyFlags.Used = isReturnValueUsed;
5728 if (RetSExt)
5729 MyFlags.Flags.setSExt();
5730 if (RetZExt)
5731 MyFlags.Flags.setZExt();
5732 if (isInreg)
5733 MyFlags.Flags.setInReg();
5734 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005735 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005736 }
5737
Dan Gohman98ca4f22009-08-05 01:29:28 +00005738 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005739 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005740 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005741
5742 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005744 "LowerCall didn't return a valid chain!");
5745 assert((!isTailCall || InVals.empty()) &&
5746 "LowerCall emitted a return value for a tail call!");
5747 assert((isTailCall || InVals.size() == Ins.size()) &&
5748 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005749
5750 // For a tail call, the return value is merely live-out and there aren't
5751 // any nodes in the DAG representing it. Return a special value to
5752 // indicate that a tail call has been emitted and no more Instructions
5753 // should be processed in the current block.
5754 if (isTailCall) {
5755 DAG.setRoot(Chain);
5756 return std::make_pair(SDValue(), SDValue());
5757 }
5758
Evan Chengaf1871f2010-03-11 19:38:18 +00005759 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5760 assert(InVals[i].getNode() &&
5761 "LowerCall emitted a null value!");
5762 assert(Ins[i].VT == InVals[i].getValueType() &&
5763 "LowerCall emitted a value with the wrong type!");
5764 });
5765
Dan Gohman98ca4f22009-08-05 01:29:28 +00005766 // Collect the legal value parts into potentially illegal values
5767 // that correspond to the original function's return values.
5768 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5769 if (RetSExt)
5770 AssertOp = ISD::AssertSext;
5771 else if (RetZExt)
5772 AssertOp = ISD::AssertZext;
5773 SmallVector<SDValue, 4> ReturnValues;
5774 unsigned CurReg = 0;
5775 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
Bill Wendling46ada192010-03-02 01:55:18 +00005780 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005781 NumRegs, RegisterVT, VT,
5782 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005783 CurReg += NumRegs;
5784 }
5785
5786 // For a function returning void, there is no return value. We can't create
5787 // such a node, so we just return a null return value in that case. In
5788 // that case, nothing will actualy look at the value.
5789 if (ReturnValues.empty())
5790 return std::make_pair(SDValue(), Chain);
5791
5792 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5793 DAG.getVTList(&RetTys[0], RetTys.size()),
5794 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005795 return std::make_pair(Res, Chain);
5796}
5797
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005798void TargetLowering::LowerOperationWrapper(SDNode *N,
5799 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005800 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005801 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005802 if (Res.getNode())
5803 Results.push_back(Res);
5804}
5805
Dan Gohmand858e902010-04-17 15:26:15 +00005806SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005807 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 return SDValue();
5809}
5810
Dan Gohman46510a72010-04-15 01:51:59 +00005811void
5812SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005813 SDValue Op = getValue(V);
5814 assert((Op.getOpcode() != ISD::CopyFromReg ||
5815 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5816 "Copy from a reg to the same reg!");
5817 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5818
Owen Anderson23b9b192009-08-12 00:36:31 +00005819 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005820 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005821 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005822 PendingExports.push_back(Chain);
5823}
5824
5825#include "llvm/CodeGen/SelectionDAGISel.h"
5826
Dan Gohman46510a72010-04-15 01:51:59 +00005827void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005828 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005829 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005830 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005831 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005832 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005833 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005834 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005835
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005836 // Check whether the function can return without sret-demotion.
5837 SmallVector<EVT, 4> OutVTs;
5838 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005839 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005840 OutVTs, OutsFlags, TLI);
5841 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5842
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005843 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005844 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005845 if (!FLI.CanLowerReturn) {
5846 // Put in an sret pointer parameter before all the other parameters.
5847 SmallVector<EVT, 1> ValueVTs;
5848 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5849
5850 // NOTE: Assuming that a pointer will never break down to more than one VT
5851 // or one register.
5852 ISD::ArgFlagsTy Flags;
5853 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005854 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005855 ISD::InputArg RetArg(Flags, RegisterVT, true);
5856 Ins.push_back(RetArg);
5857 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005858
Dan Gohman98ca4f22009-08-05 01:29:28 +00005859 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005860 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005861 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005862 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005863 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005864 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5865 bool isArgValueUsed = !I->use_empty();
5866 for (unsigned Value = 0, NumValues = ValueVTs.size();
5867 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005868 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005869 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005870 ISD::ArgFlagsTy Flags;
5871 unsigned OriginalAlignment =
5872 TD->getABITypeAlignment(ArgTy);
5873
5874 if (F.paramHasAttr(Idx, Attribute::ZExt))
5875 Flags.setZExt();
5876 if (F.paramHasAttr(Idx, Attribute::SExt))
5877 Flags.setSExt();
5878 if (F.paramHasAttr(Idx, Attribute::InReg))
5879 Flags.setInReg();
5880 if (F.paramHasAttr(Idx, Attribute::StructRet))
5881 Flags.setSRet();
5882 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5883 Flags.setByVal();
5884 const PointerType *Ty = cast<PointerType>(I->getType());
5885 const Type *ElementTy = Ty->getElementType();
5886 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5887 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5888 // For ByVal, alignment should be passed from FE. BE will guess if
5889 // this info is not there but there are cases it cannot get right.
5890 if (F.getParamAlignment(Idx))
5891 FrameAlign = F.getParamAlignment(Idx);
5892 Flags.setByValAlign(FrameAlign);
5893 Flags.setByValSize(FrameSize);
5894 }
5895 if (F.paramHasAttr(Idx, Attribute::Nest))
5896 Flags.setNest();
5897 Flags.setOrigAlign(OriginalAlignment);
5898
Owen Anderson23b9b192009-08-12 00:36:31 +00005899 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5900 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005901 for (unsigned i = 0; i != NumRegs; ++i) {
5902 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5903 if (NumRegs > 1 && i == 0)
5904 MyFlags.Flags.setSplit();
5905 // if it isn't first piece, alignment must be 1
5906 else if (i > 0)
5907 MyFlags.Flags.setOrigAlign(1);
5908 Ins.push_back(MyFlags);
5909 }
5910 }
5911 }
5912
5913 // Call the target to set up the argument values.
5914 SmallVector<SDValue, 8> InVals;
5915 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5916 F.isVarArg(), Ins,
5917 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005918
5919 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005921 "LowerFormalArguments didn't return a valid chain!");
5922 assert(InVals.size() == Ins.size() &&
5923 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005924 DEBUG({
5925 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5926 assert(InVals[i].getNode() &&
5927 "LowerFormalArguments emitted a null value!");
5928 assert(Ins[i].VT == InVals[i].getValueType() &&
5929 "LowerFormalArguments emitted a value with the wrong type!");
5930 }
5931 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005932
Dan Gohman5e866062009-08-06 15:37:27 +00005933 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005934 DAG.setRoot(NewRoot);
5935
5936 // Set up the argument values.
5937 unsigned i = 0;
5938 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005939 if (!FLI.CanLowerReturn) {
5940 // Create a virtual register for the sret pointer, and put in a copy
5941 // from the sret argument into it.
5942 SmallVector<EVT, 1> ValueVTs;
5943 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5944 EVT VT = ValueVTs[0];
5945 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5946 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005947 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005948 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005949
Dan Gohman2048b852009-11-23 18:04:58 +00005950 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005951 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5952 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5953 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005954 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5955 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005956 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005957
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005958 // i indexes lowered arguments. Bump it past the hidden sret argument.
5959 // Idx indexes LLVM arguments. Don't touch it.
5960 ++i;
5961 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005962
Dan Gohman46510a72010-04-15 01:51:59 +00005963 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005964 ++I, ++Idx) {
5965 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005966 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005967 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005968 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005969 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005970 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005971 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5972 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005973
5974 if (!I->use_empty()) {
5975 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5976 if (F.paramHasAttr(Idx, Attribute::SExt))
5977 AssertOp = ISD::AssertSext;
5978 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5979 AssertOp = ISD::AssertZext;
5980
Bill Wendling46ada192010-03-02 01:55:18 +00005981 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00005982 NumParts, PartVT, VT,
5983 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005984 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005985
Dan Gohman98ca4f22009-08-05 01:29:28 +00005986 i += NumParts;
5987 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005988
Dan Gohman98ca4f22009-08-05 01:29:28 +00005989 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00005990 SDValue Res;
5991 if (!ArgValues.empty())
5992 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
5993 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00005994 SDB->setValue(I, Res);
5995
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005996 // If this argument is live outside of the entry block, insert a copy from
5997 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005998 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006000 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006001
Dan Gohman98ca4f22009-08-05 01:29:28 +00006002 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003
6004 // Finally, if the target has anything special to do, allow it to do so.
6005 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006006 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006007}
6008
6009/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6010/// ensure constants are generated when needed. Remember the virtual registers
6011/// that need to be added to the Machine PHI nodes as input. We cannot just
6012/// directly add them, because expansion might result in multiple MBB's for one
6013/// BB. As such, the start of the BB might correspond to a different MBB than
6014/// the end.
6015///
6016void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006017SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006018 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006019
6020 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6021
6022 // Check successor nodes' PHI nodes that expect a constant to be available
6023 // from this block.
6024 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006025 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006026 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006027 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006029 // If this terminator has multiple identical successors (common for
6030 // switches), only handle each succ once.
6031 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006033 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006034
6035 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6036 // nodes and Machine PHI nodes, but the incoming operands have not been
6037 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006038 for (BasicBlock::const_iterator I = SuccBB->begin();
6039 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006040 // Ignore dead phi's.
6041 if (PN->use_empty()) continue;
6042
6043 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006044 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006045
Dan Gohman46510a72010-04-15 01:51:59 +00006046 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006047 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006048 if (RegOut == 0) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006049 RegOut = FuncInfo.CreateRegForValue(C);
6050 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006051 }
6052 Reg = RegOut;
6053 } else {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006054 Reg = FuncInfo.ValueMap[PHIOp];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006055 if (Reg == 0) {
6056 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006057 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006058 "Didn't codegen value into a register!??");
Dan Gohmanf81eca02010-04-22 20:46:50 +00006059 Reg = FuncInfo.CreateRegForValue(PHIOp);
6060 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061 }
6062 }
6063
6064 // Remember that this register needs to added to the machine PHI node as
6065 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006066 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006067 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6068 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006069 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006070 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006071 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006072 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006073 Reg += NumRegisters;
6074 }
6075 }
6076 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006077 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006078}