blob: a1842d7a1e58a99bed51d4f8b6f059c9942c51ec [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 Gohman8ba3aa72010-04-20 00:48:35 +0000618 CurDebugLoc = I.getDebugLoc();
619
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000620 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000621
Dan Gohman92884f72010-04-20 15:03:56 +0000622 if (!isa<TerminatorInst>(&I) && !HasTailCall)
623 CopyToExportRegsIfNeeded(&I);
624
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000625 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000626}
627
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000628void SelectionDAGBuilder::visitPHI(const PHINode &) {
629 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
630}
631
Dan Gohman46510a72010-04-15 01:51:59 +0000632void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000633 // Note: this doesn't use InstVisitor, because it has to work with
634 // ConstantExpr's in addition to instructions.
635 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000636 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000637 // Build the switch statement using the Instruction.def file.
638#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000639 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000640#include "llvm/Instruction.def"
641 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000642
643 // Assign the ordering to the freshly created DAG nodes.
644 if (NodeMap.count(&I)) {
645 ++SDNodeOrder;
646 AssignOrderingToNode(getValue(&I).getNode());
647 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000648}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000649
Dan Gohman2048b852009-11-23 18:04:58 +0000650SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000651 SDValue &N = NodeMap[V];
652 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000653
Dan Gohman383b5f62010-04-17 15:32:28 +0000654 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000655 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000656
Dan Gohman383b5f62010-04-17 15:32:28 +0000657 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000658 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000659
Dan Gohman383b5f62010-04-17 15:32:28 +0000660 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000661 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663 if (isa<ConstantPointerNull>(C))
664 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000665
Dan Gohman383b5f62010-04-17 15:32:28 +0000666 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000667 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000668
Nate Begeman9008ca62009-04-27 18:41:29 +0000669 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000670 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000671
Dan Gohman383b5f62010-04-17 15:32:28 +0000672 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673 visit(CE->getOpcode(), *CE);
674 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000675 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000676 return N1;
677 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000679 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
680 SmallVector<SDValue, 4> Constants;
681 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
682 OI != OE; ++OI) {
683 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000684 // If the operand is an empty aggregate, there are no values.
685 if (!Val) continue;
686 // Add each leaf value from the operand to the Constants list
687 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000688 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
689 Constants.push_back(SDValue(Val, i));
690 }
Bill Wendling87710f02009-12-21 23:47:40 +0000691
Bill Wendling4533cac2010-01-28 21:51:40 +0000692 return DAG.getMergeValues(&Constants[0], Constants.size(),
693 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694 }
695
Duncan Sands1df98592010-02-16 11:11:14 +0000696 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000697 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
698 "Unknown struct or array constant!");
699
Owen Andersone50ed302009-08-10 22:56:29 +0000700 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000701 ComputeValueVTs(TLI, C->getType(), ValueVTs);
702 unsigned NumElts = ValueVTs.size();
703 if (NumElts == 0)
704 return SDValue(); // empty struct
705 SmallVector<SDValue, 4> Constants(NumElts);
706 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000707 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000708 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000709 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000710 else if (EltVT.isFloatingPoint())
711 Constants[i] = DAG.getConstantFP(0, EltVT);
712 else
713 Constants[i] = DAG.getConstant(0, EltVT);
714 }
Bill Wendling87710f02009-12-21 23:47:40 +0000715
Bill Wendling4533cac2010-01-28 21:51:40 +0000716 return DAG.getMergeValues(&Constants[0], NumElts,
717 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000718 }
719
Dan Gohman383b5f62010-04-17 15:32:28 +0000720 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000721 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000722
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000723 const VectorType *VecTy = cast<VectorType>(V->getType());
724 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000726 // Now that we know the number and type of the elements, get that number of
727 // elements into the Ops array based on what kind of constant it is.
728 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000729 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000730 for (unsigned i = 0; i != NumElements; ++i)
731 Ops.push_back(getValue(CP->getOperand(i)));
732 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000733 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000734 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000735
736 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000737 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000738 Op = DAG.getConstantFP(0, EltVT);
739 else
740 Op = DAG.getConstant(0, EltVT);
741 Ops.assign(NumElements, Op);
742 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000743
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000744 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000745 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
746 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000747 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000748
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000749 // If this is a static alloca, generate it as the frameindex instead of
750 // computation.
751 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
752 DenseMap<const AllocaInst*, int>::iterator SI =
753 FuncInfo.StaticAllocaMap.find(AI);
754 if (SI != FuncInfo.StaticAllocaMap.end())
755 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
756 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000758 unsigned InReg = FuncInfo.ValueMap[V];
759 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000760
Owen Anderson23b9b192009-08-12 00:36:31 +0000761 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000762 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000763 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764}
765
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000766/// Get the EVTs and ArgFlags collections that represent the legalized return
767/// type of the given function. This does not require a DAG or a return value,
768/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000769static void getReturnInfo(const Type* ReturnType,
770 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000771 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000772 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000773 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000774 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000775 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000776 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000777 if (NumValues == 0) return;
778 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000779
780 for (unsigned j = 0, f = NumValues; j != f; ++j) {
781 EVT VT = ValueVTs[j];
782 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000783
784 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000785 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000786 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000787 ExtendKind = ISD::ZERO_EXTEND;
788
789 // FIXME: C calling convention requires the return type to be promoted to
790 // at least 32-bit. But this is not necessary for non-C calling
791 // conventions. The frontend should mark functions whose return values
792 // require promoting with signext or zeroext attributes.
793 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000794 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000795 if (VT.bitsLT(MinVT))
796 VT = MinVT;
797 }
798
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000799 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
800 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000801 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
802 PartVT.getTypeForEVT(ReturnType->getContext()));
803
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000804 // 'inreg' on function refers to return value
805 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000806 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000807 Flags.setInReg();
808
809 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000810 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000811 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000812 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000813 Flags.setZExt();
814
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000815 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000816 OutVTs.push_back(PartVT);
817 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000818 if (Offsets)
819 {
820 Offsets->push_back(Offset);
821 Offset += PartSize;
822 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000823 }
824 }
825}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000826
Dan Gohman46510a72010-04-15 01:51:59 +0000827void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000828 SDValue Chain = getControlRoot();
829 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000830 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000831
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000832 if (!FLI.CanLowerReturn) {
833 unsigned DemoteReg = FLI.DemoteRegister;
834 const Function *F = I.getParent()->getParent();
835
836 // Emit a store of the return value through the virtual register.
837 // Leave Outs empty so that LowerReturn won't try to load return
838 // registers the usual way.
839 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000840 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000841 PtrValueVTs);
842
843 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
844 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000845
Owen Andersone50ed302009-08-10 22:56:29 +0000846 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000847 SmallVector<uint64_t, 4> Offsets;
848 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000849 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000850
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000851 SmallVector<SDValue, 4> Chains(NumValues);
852 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000853 for (unsigned i = 0; i != NumValues; ++i) {
854 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
855 DAG.getConstant(Offsets[i], PtrVT));
856 Chains[i] =
857 DAG.getStore(Chain, getCurDebugLoc(),
858 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000859 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000860 }
861
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000862 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
863 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000864 } else if (I.getNumOperands() != 0) {
865 SmallVector<EVT, 4> ValueVTs;
866 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
867 unsigned NumValues = ValueVTs.size();
868 if (NumValues) {
869 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000870 for (unsigned j = 0, f = NumValues; j != f; ++j) {
871 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000872
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000873 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000874
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000875 const Function *F = I.getParent()->getParent();
876 if (F->paramHasAttr(0, Attribute::SExt))
877 ExtendKind = ISD::SIGN_EXTEND;
878 else if (F->paramHasAttr(0, Attribute::ZExt))
879 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000880
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000881 // FIXME: C calling convention requires the return type to be promoted
882 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000883 // conventions. The frontend should mark functions whose return values
884 // require promoting with signext or zeroext attributes.
885 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
886 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
887 if (VT.bitsLT(MinVT))
888 VT = MinVT;
889 }
890
891 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
892 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
893 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000894 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000895 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
896 &Parts[0], NumParts, PartVT, ExtendKind);
897
898 // 'inreg' on function refers to return value
899 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
900 if (F->paramHasAttr(0, Attribute::InReg))
901 Flags.setInReg();
902
903 // Propagate extension type if any
904 if (F->paramHasAttr(0, Attribute::SExt))
905 Flags.setSExt();
906 else if (F->paramHasAttr(0, Attribute::ZExt))
907 Flags.setZExt();
908
909 for (unsigned i = 0; i < NumParts; ++i)
910 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000911 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000912 }
913 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000914
915 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000916 CallingConv::ID CallConv =
917 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000918 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
919 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000920
921 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000923 "LowerReturn didn't return a valid chain!");
924
925 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000926 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000927}
928
Dan Gohmanad62f532009-04-23 23:13:24 +0000929/// CopyToExportRegsIfNeeded - If the given value has virtual registers
930/// created for it, emit nodes to copy the value into the virtual
931/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000932void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000933 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
934 if (VMI != FuncInfo.ValueMap.end()) {
935 assert(!V->use_empty() && "Unused value assigned virtual registers!");
936 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000937 }
938}
939
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000940/// ExportFromCurrentBlock - If this condition isn't known to be exported from
941/// the current basic block, add it to ValueMap now so that we'll get a
942/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000943void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000944 // No need to export constants.
945 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000946
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 // Already exported?
948 if (FuncInfo.isExportedInst(V)) return;
949
950 unsigned Reg = FuncInfo.InitializeRegForValue(V);
951 CopyValueToVirtualRegister(V, Reg);
952}
953
Dan Gohman46510a72010-04-15 01:51:59 +0000954bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000955 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000956 // The operands of the setcc have to be in this block. We don't know
957 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000958 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000959 // Can export from current BB.
960 if (VI->getParent() == FromBB)
961 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000962
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000963 // Is already exported, noop.
964 return FuncInfo.isExportedInst(V);
965 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000967 // If this is an argument, we can export it if the BB is the entry block or
968 // if it is already exported.
969 if (isa<Argument>(V)) {
970 if (FromBB == &FromBB->getParent()->getEntryBlock())
971 return true;
972
973 // Otherwise, can only export this if it is already exported.
974 return FuncInfo.isExportedInst(V);
975 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000977 // Otherwise, constants can always be exported.
978 return true;
979}
980
981static bool InBlock(const Value *V, const BasicBlock *BB) {
982 if (const Instruction *I = dyn_cast<Instruction>(V))
983 return I->getParent() == BB;
984 return true;
985}
986
Dan Gohmanc2277342008-10-17 21:16:08 +0000987/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
988/// This function emits a branch and is used at the leaves of an OR or an
989/// AND operator tree.
990///
991void
Dan Gohman46510a72010-04-15 01:51:59 +0000992SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000993 MachineBasicBlock *TBB,
994 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000995 MachineBasicBlock *CurBB,
996 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000997 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000998
Dan Gohmanc2277342008-10-17 21:16:08 +0000999 // If the leaf of the tree is a comparison, merge the condition into
1000 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001001 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001002 // The operands of the cmp have to be in this block. We don't know
1003 // how to export them from some other block. If this is the first block
1004 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001005 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001006 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1007 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001009 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001010 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001011 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001012 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001013 } else {
1014 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001015 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001017
1018 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001019 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1020 SwitchCases.push_back(CB);
1021 return;
1022 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001023 }
1024
1025 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001026 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001027 NULL, TBB, FBB, CurBB);
1028 SwitchCases.push_back(CB);
1029}
1030
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001031/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001032void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001033 MachineBasicBlock *TBB,
1034 MachineBasicBlock *FBB,
1035 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001036 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001037 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001038 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001039 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001040 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001041 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1042 BOp->getParent() != CurBB->getBasicBlock() ||
1043 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1044 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001045 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001046 return;
1047 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001048
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001049 // Create TmpBB after CurBB.
1050 MachineFunction::iterator BBI = CurBB;
1051 MachineFunction &MF = DAG.getMachineFunction();
1052 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1053 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001055 if (Opc == Instruction::Or) {
1056 // Codegen X | Y as:
1057 // jmp_if_X TBB
1058 // jmp TmpBB
1059 // TmpBB:
1060 // jmp_if_Y TBB
1061 // jmp FBB
1062 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001063
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001064 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001065 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001067 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001068 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001069 } else {
1070 assert(Opc == Instruction::And && "Unknown merge op!");
1071 // Codegen X & Y as:
1072 // jmp_if_X TmpBB
1073 // jmp FBB
1074 // TmpBB:
1075 // jmp_if_Y TBB
1076 // jmp FBB
1077 //
1078 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001079
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001080 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001081 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001084 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001085 }
1086}
1087
1088/// If the set of cases should be emitted as a series of branches, return true.
1089/// If we should emit this as a bunch of and/or'd together conditions, return
1090/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001091bool
Dan Gohman2048b852009-11-23 18:04:58 +00001092SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001093 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 // If this is two comparisons of the same values or'd or and'd together, they
1096 // will get folded into a single comparison, so don't emit two blocks.
1097 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1098 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1099 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1100 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1101 return false;
1102 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001103
Chris Lattner133ce872010-01-02 00:00:03 +00001104 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1105 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1106 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1107 Cases[0].CC == Cases[1].CC &&
1108 isa<Constant>(Cases[0].CmpRHS) &&
1109 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1110 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1111 return false;
1112 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1113 return false;
1114 }
1115
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001116 return true;
1117}
1118
Dan Gohman46510a72010-04-15 01:51:59 +00001119void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001120 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1121
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001122 // Update machine-CFG edges.
1123 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1124
1125 // Figure out which block is immediately after the current one.
1126 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001127 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001128 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001129 NextBlock = BBI;
1130
1131 if (I.isUnconditional()) {
1132 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001133 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001134
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001135 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001136 if (Succ0MBB != NextBlock)
1137 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001139 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141 return;
1142 }
1143
1144 // If this condition is one of the special cases we handle, do special stuff
1145 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001146 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001147 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1148
1149 // If this is a series of conditions that are or'd or and'd together, emit
1150 // this as a sequence of branches instead of setcc's with and/or operations.
1151 // For example, instead of something like:
1152 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001153 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001154 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001155 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001156 // or C, F
1157 // jnz foo
1158 // Emit:
1159 // cmp A, B
1160 // je foo
1161 // cmp D, E
1162 // jle foo
1163 //
Dan Gohman46510a72010-04-15 01:51:59 +00001164 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001165 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001166 (BOp->getOpcode() == Instruction::And ||
1167 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001168 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1169 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001170 // If the compares in later blocks need to use values not currently
1171 // exported from this block, export them now. This block should always
1172 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001173 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001174
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001175 // Allow some cases to be rejected.
1176 if (ShouldEmitAsBranches(SwitchCases)) {
1177 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1178 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1179 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001182 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001183 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001184 SwitchCases.erase(SwitchCases.begin());
1185 return;
1186 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001188 // Okay, we decided not to do this, remove any inserted MBB's and clear
1189 // SwitchCases.
1190 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001191 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 SwitchCases.clear();
1194 }
1195 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001197 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001198 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001199 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001200
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001201 // Use visitSwitchCase to actually insert the fast branch sequence for this
1202 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001203 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001204}
1205
1206/// visitSwitchCase - Emits the necessary code to represent a single node in
1207/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001208void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1209 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001210 SDValue Cond;
1211 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001212 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001213
1214 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 if (CB.CmpMHS == NULL) {
1216 // Fold "(X == true)" to X and "(X == false)" to !X to
1217 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001218 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001219 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001221 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001222 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001224 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001225 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227 } else {
1228 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1229
Anton Korobeynikov23218582008-12-23 22:25:27 +00001230 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1231 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001232
1233 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001234 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001235
1236 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001238 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001240 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001241 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001243 DAG.getConstant(High-Low, VT), ISD::SETULE);
1244 }
1245 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001246
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001248 SwitchBB->addSuccessor(CB.TrueBB);
1249 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 // Set NextBlock to be the MBB immediately after the current one, if any.
1252 // This is used to avoid emitting unnecessary branches to the next block.
1253 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001254 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001255 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001258 // If the lhs block is the next block, invert the condition so that we can
1259 // fall through to the lhs instead of the rhs block.
1260 if (CB.TrueBB == NextBlock) {
1261 std::swap(CB.TrueBB, CB.FalseBB);
1262 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001263 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001264 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001265
Dale Johannesenf5d97892009-02-04 01:48:28 +00001266 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001268 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001270 // If the branch was constant folded, fix up the CFG.
1271 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001272 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001273 } else {
1274 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001275 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001276 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001277
Bill Wendling4533cac2010-01-28 21:51:40 +00001278 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001279 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1280 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001281 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001282
1283 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001284}
1285
1286/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001287void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288 // Emit the code for the jump table
1289 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001290 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001291 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1292 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001293 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001294 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1295 MVT::Other, Index.getValue(1),
1296 Table, Index);
1297 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001298}
1299
1300/// visitJumpTableHeader - This function emits necessary code to produce index
1301/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001302void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001303 JumpTableHeader &JTH,
1304 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001305 // Subtract the lowest switch case value from the value being switched on and
1306 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001307 // difference between smallest and largest cases.
1308 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001309 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001310 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001311 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001312
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001313 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001314 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001315 // can be used as an index into the jump table in a subsequent basic block.
1316 // This value may be smaller or larger than the target's pointer type, and
1317 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001318 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001319
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001321 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1322 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001323 JT.Reg = JumpTableReg;
1324
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001325 // Emit the range check for the jump table, and branch to the default block
1326 // for the switch statement if the value being switched on exceeds the largest
1327 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001328 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001329 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001330 DAG.getConstant(JTH.Last-JTH.First,VT),
1331 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001332
1333 // Set NextBlock to be the MBB immediately after the current one, if any.
1334 // This is used to avoid emitting unnecessary branches to the next block.
1335 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001336 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001337
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001338 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001339 NextBlock = BBI;
1340
Dale Johannesen66978ee2009-01-31 02:22:37 +00001341 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001343 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344
Bill Wendling4533cac2010-01-28 21:51:40 +00001345 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001346 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1347 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001348
Bill Wendling87710f02009-12-21 23:47:40 +00001349 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350}
1351
1352/// visitBitTestHeader - This function emits necessary code to produce value
1353/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001354void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1355 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001356 // Subtract the minimum value
1357 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001358 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001359 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001360 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001361
1362 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001363 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001364 TLI.getSetCCResultType(Sub.getValueType()),
1365 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001366 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001367
Bill Wendling87710f02009-12-21 23:47:40 +00001368 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1369 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001370
Duncan Sands92abc622009-01-31 15:50:11 +00001371 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001372 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1373 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374
1375 // Set NextBlock to be the MBB immediately after the current one, if any.
1376 // This is used to avoid emitting unnecessary branches to the next block.
1377 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001378 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001379 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001380 NextBlock = BBI;
1381
1382 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1383
Dan Gohman99be8ae2010-04-19 22:41:47 +00001384 SwitchBB->addSuccessor(B.Default);
1385 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386
Dale Johannesen66978ee2009-01-31 02:22:37 +00001387 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001388 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001389 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001390
Bill Wendling4533cac2010-01-28 21:51:40 +00001391 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001392 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1393 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001394
Bill Wendling87710f02009-12-21 23:47:40 +00001395 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396}
1397
1398/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001399void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1400 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001401 BitTestCase &B,
1402 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001403 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001404 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001405 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001406 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001407 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001408 DAG.getConstant(1, TLI.getPointerTy()),
1409 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001410
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001411 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001412 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001413 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001414 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001415 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1416 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001417 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001418 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419
Dan Gohman99be8ae2010-04-19 22:41:47 +00001420 SwitchBB->addSuccessor(B.TargetBB);
1421 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001422
Dale Johannesen66978ee2009-01-31 02:22:37 +00001423 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001424 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001425 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001426
1427 // Set NextBlock to be the MBB immediately after the current one, if any.
1428 // This is used to avoid emitting unnecessary branches to the next block.
1429 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001430 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001431 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001432 NextBlock = BBI;
1433
Bill Wendling4533cac2010-01-28 21:51:40 +00001434 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001435 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1436 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001437
Bill Wendling87710f02009-12-21 23:47:40 +00001438 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439}
1440
Dan Gohman46510a72010-04-15 01:51:59 +00001441void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001442 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001444 // Retrieve successors.
1445 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1446 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1447
Gabor Greifb67e6b32009-01-15 11:10:44 +00001448 const Value *Callee(I.getCalledValue());
1449 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001450 visitInlineAsm(&I);
1451 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001452 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453
1454 // If the value of the invoke is used outside of its defining block, make it
1455 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001456 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457
1458 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001459 InvokeMBB->addSuccessor(Return);
1460 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001461
1462 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001463 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1464 MVT::Other, getControlRoot(),
1465 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001466}
1467
Dan Gohman46510a72010-04-15 01:51:59 +00001468void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001469}
1470
1471/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1472/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001473bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1474 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001475 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001476 MachineBasicBlock *Default,
1477 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001478 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001481 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001483 return false;
1484
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485 // Get the MachineFunction which holds the current MBB. This is used when
1486 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001487 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001488
1489 // Figure out which block is immediately after the current one.
1490 MachineBasicBlock *NextBlock = 0;
1491 MachineFunction::iterator BBI = CR.CaseBB;
1492
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001493 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001494 NextBlock = BBI;
1495
1496 // TODO: If any two of the cases has the same destination, and if one value
1497 // is the same as the other, but has one bit unset that the other has set,
1498 // use bit manipulation to do two compares at once. For example:
1499 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001500
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001501 // Rearrange the case blocks so that the last one falls through if possible.
1502 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1503 // The last case block won't fall through into 'NextBlock' if we emit the
1504 // branches in this order. See if rearranging a case value would help.
1505 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1506 if (I->BB == NextBlock) {
1507 std::swap(*I, BackCase);
1508 break;
1509 }
1510 }
1511 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001512
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001513 // Create a CaseBlock record representing a conditional branch to
1514 // the Case's target mbb if the value being switched on SV is equal
1515 // to C.
1516 MachineBasicBlock *CurBlock = CR.CaseBB;
1517 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1518 MachineBasicBlock *FallThrough;
1519 if (I != E-1) {
1520 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1521 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001522
1523 // Put SV in a virtual register to make it available from the new blocks.
1524 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001525 } else {
1526 // If the last case doesn't match, go to the default block.
1527 FallThrough = Default;
1528 }
1529
Dan Gohman46510a72010-04-15 01:51:59 +00001530 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001531 ISD::CondCode CC;
1532 if (I->High == I->Low) {
1533 // This is just small small case range :) containing exactly 1 case
1534 CC = ISD::SETEQ;
1535 LHS = SV; RHS = I->High; MHS = NULL;
1536 } else {
1537 CC = ISD::SETLE;
1538 LHS = I->Low; MHS = SV; RHS = I->High;
1539 }
1540 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001541
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 // If emitting the first comparison, just call visitSwitchCase to emit the
1543 // code into the current block. Otherwise, push the CaseBlock onto the
1544 // vector to be later processed by SDISel, and insert the node's MBB
1545 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001546 if (CurBlock == SwitchBB)
1547 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001548 else
1549 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 CurBlock = FallThrough;
1552 }
1553
1554 return true;
1555}
1556
1557static inline bool areJTsAllowed(const TargetLowering &TLI) {
1558 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1560 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001562
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001563static APInt ComputeRange(const APInt &First, const APInt &Last) {
1564 APInt LastExt(Last), FirstExt(First);
1565 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1566 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1567 return (LastExt - FirstExt + 1ULL);
1568}
1569
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001570/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001571bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1572 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001573 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001574 MachineBasicBlock* Default,
1575 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576 Case& FrontCase = *CR.Range.first;
1577 Case& BackCase = *(CR.Range.second-1);
1578
Chris Lattnere880efe2009-11-07 07:50:34 +00001579 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1580 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581
Chris Lattnere880efe2009-11-07 07:50:34 +00001582 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001583 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1584 I!=E; ++I)
1585 TSize += I->size();
1586
Dan Gohmane0567812010-04-08 23:03:40 +00001587 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001588 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001589
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001590 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001591 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001592 if (Density < 0.4)
1593 return false;
1594
David Greene4b69d992010-01-05 01:24:57 +00001595 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001596 << "First entry: " << First << ". Last entry: " << Last << '\n'
1597 << "Range: " << Range
1598 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001599
1600 // Get the MachineFunction which holds the current MBB. This is used when
1601 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001602 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001603
1604 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001606 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607
1608 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1609
1610 // Create a new basic block to hold the code for loading the address
1611 // of the jump table, and jumping to it. Update successor information;
1612 // we will either branch to the default case for the switch, or the jump
1613 // table.
1614 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1615 CurMF->insert(BBI, JumpTableBB);
1616 CR.CaseBB->addSuccessor(Default);
1617 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 // Build a vector of destination BBs, corresponding to each target
1620 // of the jump table. If the value of the jump table slot corresponds to
1621 // a case statement, push the case's BB onto the vector, otherwise, push
1622 // the default BB.
1623 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001624 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001625 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001626 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1627 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001628
1629 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001630 DestBBs.push_back(I->BB);
1631 if (TEI==High)
1632 ++I;
1633 } else {
1634 DestBBs.push_back(Default);
1635 }
1636 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1640 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 E = DestBBs.end(); I != E; ++I) {
1642 if (!SuccsHandled[(*I)->getNumber()]) {
1643 SuccsHandled[(*I)->getNumber()] = true;
1644 JumpTableBB->addSuccessor(*I);
1645 }
1646 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001647
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001648 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001649 unsigned JTEncoding = TLI.getJumpTableEncoding();
1650 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001651 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 // Set the jump table information so that we can codegen it as a second
1654 // MachineBasicBlock
1655 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001656 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1657 if (CR.CaseBB == SwitchBB)
1658 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001659
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660 JTCases.push_back(JumpTableBlock(JTH, JT));
1661
1662 return true;
1663}
1664
1665/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1666/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001667bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1668 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001669 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001670 MachineBasicBlock *Default,
1671 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672 // Get the MachineFunction which holds the current MBB. This is used when
1673 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001674 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001675
1676 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001677 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001678 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679
1680 Case& FrontCase = *CR.Range.first;
1681 Case& BackCase = *(CR.Range.second-1);
1682 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1683
1684 // Size is the number of Cases represented by this range.
1685 unsigned Size = CR.Range.second - CR.Range.first;
1686
Chris Lattnere880efe2009-11-07 07:50:34 +00001687 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1688 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001689 double FMetric = 0;
1690 CaseItr Pivot = CR.Range.first + Size/2;
1691
1692 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1693 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001694 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001695 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1696 I!=E; ++I)
1697 TSize += I->size();
1698
Chris Lattnere880efe2009-11-07 07:50:34 +00001699 APInt LSize = FrontCase.size();
1700 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001701 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001702 << "First: " << First << ", Last: " << Last <<'\n'
1703 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1705 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001706 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1707 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001708 APInt Range = ComputeRange(LEnd, RBegin);
1709 assert((Range - 2ULL).isNonNegative() &&
1710 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001711 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001712 (LEnd - First + 1ULL).roundToDouble();
1713 double RDensity = (double)RSize.roundToDouble() /
1714 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001715 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001716 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001717 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001718 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1719 << "LDensity: " << LDensity
1720 << ", RDensity: " << RDensity << '\n'
1721 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001722 if (FMetric < Metric) {
1723 Pivot = J;
1724 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001725 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 }
1727
1728 LSize += J->size();
1729 RSize -= J->size();
1730 }
1731 if (areJTsAllowed(TLI)) {
1732 // If our case is dense we *really* should handle it earlier!
1733 assert((FMetric > 0) && "Should handle dense range earlier!");
1734 } else {
1735 Pivot = CR.Range.first + Size/2;
1736 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001737
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001738 CaseRange LHSR(CR.Range.first, Pivot);
1739 CaseRange RHSR(Pivot, CR.Range.second);
1740 Constant *C = Pivot->Low;
1741 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001742
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001744 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001746 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001747 // Pivot's Value, then we can branch directly to the LHS's Target,
1748 // rather than creating a leaf node for it.
1749 if ((LHSR.second - LHSR.first) == 1 &&
1750 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001751 cast<ConstantInt>(C)->getValue() ==
1752 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001753 TrueBB = LHSR.first->BB;
1754 } else {
1755 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1756 CurMF->insert(BBI, TrueBB);
1757 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001758
1759 // Put SV in a virtual register to make it available from the new blocks.
1760 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001762
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001763 // Similar to the optimization above, if the Value being switched on is
1764 // known to be less than the Constant CR.LT, and the current Case Value
1765 // is CR.LT - 1, then we can branch directly to the target block for
1766 // the current Case Value, rather than emitting a RHS leaf node for it.
1767 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001768 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1769 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770 FalseBB = RHSR.first->BB;
1771 } else {
1772 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1773 CurMF->insert(BBI, FalseBB);
1774 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001775
1776 // Put SV in a virtual register to make it available from the new blocks.
1777 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001778 }
1779
1780 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001781 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001782 // Otherwise, branch to LHS.
1783 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1784
Dan Gohman99be8ae2010-04-19 22:41:47 +00001785 if (CR.CaseBB == SwitchBB)
1786 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 else
1788 SwitchCases.push_back(CB);
1789
1790 return true;
1791}
1792
1793/// handleBitTestsSwitchCase - if current case range has few destination and
1794/// range span less, than machine word bitwidth, encode case range into series
1795/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001796bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1797 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001798 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001799 MachineBasicBlock* Default,
1800 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001802 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803
1804 Case& FrontCase = *CR.Range.first;
1805 Case& BackCase = *(CR.Range.second-1);
1806
1807 // Get the MachineFunction which holds the current MBB. This is used when
1808 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001809 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001811 // If target does not have legal shift left, do not emit bit tests at all.
1812 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1813 return false;
1814
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1817 I!=E; ++I) {
1818 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001819 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001820 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001822 // Count unique destinations
1823 SmallSet<MachineBasicBlock*, 4> Dests;
1824 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1825 Dests.insert(I->BB);
1826 if (Dests.size() > 3)
1827 // Don't bother the code below, if there are too much unique destinations
1828 return false;
1829 }
David Greene4b69d992010-01-05 01:24:57 +00001830 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001831 << Dests.size() << '\n'
1832 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001833
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001834 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001835 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1836 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001837 APInt cmpRange = maxValue - minValue;
1838
David Greene4b69d992010-01-05 01:24:57 +00001839 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001840 << "Low bound: " << minValue << '\n'
1841 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001842
Dan Gohmane0567812010-04-08 23:03:40 +00001843 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 (!(Dests.size() == 1 && numCmps >= 3) &&
1845 !(Dests.size() == 2 && numCmps >= 5) &&
1846 !(Dests.size() >= 3 && numCmps >= 6)))
1847 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001848
David Greene4b69d992010-01-05 01:24:57 +00001849 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001850 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1851
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001852 // Optimize the case where all the case values fit in a
1853 // word without having to subtract minValue. In this case,
1854 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001855 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001856 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001858 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001859 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001860
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001861 CaseBitsVector CasesBits;
1862 unsigned i, count = 0;
1863
1864 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1865 MachineBasicBlock* Dest = I->BB;
1866 for (i = 0; i < count; ++i)
1867 if (Dest == CasesBits[i].BB)
1868 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001869
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001870 if (i == count) {
1871 assert((count < 3) && "Too much destinations to test!");
1872 CasesBits.push_back(CaseBits(0, Dest, 0));
1873 count++;
1874 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001875
1876 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1877 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1878
1879 uint64_t lo = (lowValue - lowBound).getZExtValue();
1880 uint64_t hi = (highValue - lowBound).getZExtValue();
1881
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 for (uint64_t j = lo; j <= hi; j++) {
1883 CasesBits[i].Mask |= 1ULL << j;
1884 CasesBits[i].Bits++;
1885 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001886
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001887 }
1888 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001889
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890 BitTestInfo BTC;
1891
1892 // Figure out which block is immediately after the current one.
1893 MachineFunction::iterator BBI = CR.CaseBB;
1894 ++BBI;
1895
1896 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1897
David Greene4b69d992010-01-05 01:24:57 +00001898 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001900 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001901 << ", Bits: " << CasesBits[i].Bits
1902 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903
1904 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1905 CurMF->insert(BBI, CaseBB);
1906 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1907 CaseBB,
1908 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001909
1910 // Put SV in a virtual register to make it available from the new blocks.
1911 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001912 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001913
1914 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001915 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001916 CR.CaseBB, Default, BTC);
1917
Dan Gohman99be8ae2010-04-19 22:41:47 +00001918 if (CR.CaseBB == SwitchBB)
1919 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001920
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 BitTestCases.push_back(BTB);
1922
1923 return true;
1924}
1925
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001926/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001927size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1928 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001929 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930
1931 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001932 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1934 Cases.push_back(Case(SI.getSuccessorValue(i),
1935 SI.getSuccessorValue(i),
1936 SMBB));
1937 }
1938 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1939
1940 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001941 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001942 // Must recompute end() each iteration because it may be
1943 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001944 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1945 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1946 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001947 MachineBasicBlock* nextBB = J->BB;
1948 MachineBasicBlock* currentBB = I->BB;
1949
1950 // If the two neighboring cases go to the same destination, merge them
1951 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001952 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953 I->High = J->High;
1954 J = Cases.erase(J);
1955 } else {
1956 I = J++;
1957 }
1958 }
1959
1960 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1961 if (I->Low != I->High)
1962 // A range counts double, since it requires two compares.
1963 ++numCmps;
1964 }
1965
1966 return numCmps;
1967}
1968
Dan Gohman46510a72010-04-15 01:51:59 +00001969void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001970 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972 // Figure out which block is immediately after the current one.
1973 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001974 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1975
1976 // If there is only the default destination, branch to it if it is not the
1977 // next basic block. Otherwise, just fall through.
1978 if (SI.getNumOperands() == 2) {
1979 // Update machine-CFG edges.
1980
1981 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001982 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001983 if (Default != NextBlock)
1984 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1985 MVT::Other, getControlRoot(),
1986 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001988 return;
1989 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001991 // If there are any non-default case statements, create a vector of Cases
1992 // representing each one, and sort the vector so that we can efficiently
1993 // create a binary search tree from them.
1994 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001995 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001996 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001997 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001998 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001999
2000 // Get the Value to be switched on and default basic blocks, which will be
2001 // inserted into CaseBlock records, representing basic blocks in the binary
2002 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002003 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002004
2005 // Push the initial CaseRec onto the worklist
2006 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002007 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2008 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002009
2010 while (!WorkList.empty()) {
2011 // Grab a record representing a case range to process off the worklist
2012 CaseRec CR = WorkList.back();
2013 WorkList.pop_back();
2014
Dan Gohman99be8ae2010-04-19 22:41:47 +00002015 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002016 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002017
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002018 // If the range has few cases (two or less) emit a series of specific
2019 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002020 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002022
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002023 // If the switch has more than 5 blocks, and at least 40% dense, and the
2024 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002026 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002027 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002029 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2030 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002031 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002032 }
2033}
2034
Dan Gohman46510a72010-04-15 01:51:59 +00002035void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002036 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2037
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002038 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002039 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002040 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002041 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002042 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002043 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002044 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2045 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002046 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002047
Bill Wendling4533cac2010-01-28 21:51:40 +00002048 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2049 MVT::Other, getControlRoot(),
2050 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002051}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052
Dan Gohman46510a72010-04-15 01:51:59 +00002053void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002054 // -0.0 - X --> fneg
2055 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002056 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002057 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2058 const VectorType *DestTy = cast<VectorType>(I.getType());
2059 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002060 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002061 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002062 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002063 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002064 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002065 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2066 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002067 return;
2068 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002069 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002070 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002071
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002072 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002073 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002074 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002075 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2076 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002077 return;
2078 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002079
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002080 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081}
2082
Dan Gohman46510a72010-04-15 01:51:59 +00002083void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084 SDValue Op1 = getValue(I.getOperand(0));
2085 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002086 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2087 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002088}
2089
Dan Gohman46510a72010-04-15 01:51:59 +00002090void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002091 SDValue Op1 = getValue(I.getOperand(0));
2092 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002093 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002094 Op2.getValueType() != TLI.getShiftAmountTy()) {
2095 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002096 EVT PTy = TLI.getPointerTy();
2097 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002098 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002099 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2100 TLI.getShiftAmountTy(), Op2);
2101 // If the operand is larger than the shift count type but the shift
2102 // count type has enough bits to represent any shift value, truncate
2103 // it now. This is a common case and it exposes the truncate to
2104 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002105 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002106 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2107 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2108 TLI.getShiftAmountTy(), Op2);
2109 // Otherwise we'll need to temporarily settle for some other
2110 // convenient type; type legalization will make adjustments as
2111 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002112 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002113 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002114 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002115 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002116 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002117 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002118 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002119
Bill Wendling4533cac2010-01-28 21:51:40 +00002120 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2121 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122}
2123
Dan Gohman46510a72010-04-15 01:51:59 +00002124void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002125 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002126 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002128 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 predicate = ICmpInst::Predicate(IC->getPredicate());
2130 SDValue Op1 = getValue(I.getOperand(0));
2131 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002132 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002133
Owen Andersone50ed302009-08-10 22:56:29 +00002134 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002135 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002136}
2137
Dan Gohman46510a72010-04-15 01:51:59 +00002138void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002139 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002140 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002142 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143 predicate = FCmpInst::Predicate(FC->getPredicate());
2144 SDValue Op1 = getValue(I.getOperand(0));
2145 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002146 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002147 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002148 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002149}
2150
Dan Gohman46510a72010-04-15 01:51:59 +00002151void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002152 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002153 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2154 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002155 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002156
Bill Wendling49fcff82009-12-21 22:30:11 +00002157 SmallVector<SDValue, 4> Values(NumValues);
2158 SDValue Cond = getValue(I.getOperand(0));
2159 SDValue TrueVal = getValue(I.getOperand(1));
2160 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002161
Bill Wendling4533cac2010-01-28 21:51:40 +00002162 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002163 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002164 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2165 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002166 SDValue(TrueVal.getNode(),
2167 TrueVal.getResNo() + i),
2168 SDValue(FalseVal.getNode(),
2169 FalseVal.getResNo() + i));
2170
Bill Wendling4533cac2010-01-28 21:51:40 +00002171 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2172 DAG.getVTList(&ValueVTs[0], NumValues),
2173 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002174}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002175
Dan Gohman46510a72010-04-15 01:51:59 +00002176void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002177 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2178 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002179 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002180 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181}
2182
Dan Gohman46510a72010-04-15 01:51:59 +00002183void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002184 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2185 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2186 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002188 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189}
2190
Dan Gohman46510a72010-04-15 01:51:59 +00002191void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2193 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2194 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002195 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002196 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197}
2198
Dan Gohman46510a72010-04-15 01:51:59 +00002199void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002200 // FPTrunc is never a no-op cast, no need to check
2201 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002203 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2204 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205}
2206
Dan Gohman46510a72010-04-15 01:51:59 +00002207void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 // FPTrunc is never a no-op cast, no need to check
2209 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002210 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002211 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212}
2213
Dan Gohman46510a72010-04-15 01:51:59 +00002214void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002215 // FPToUI is never a no-op cast, no need to check
2216 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002218 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002219}
2220
Dan Gohman46510a72010-04-15 01:51:59 +00002221void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222 // FPToSI is never a no-op cast, no need to check
2223 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002224 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002225 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226}
2227
Dan Gohman46510a72010-04-15 01:51:59 +00002228void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229 // UIToFP is never a no-op cast, no need to check
2230 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002231 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002232 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002233}
2234
Dan Gohman46510a72010-04-15 01:51:59 +00002235void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002236 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002237 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002238 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002239 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240}
2241
Dan Gohman46510a72010-04-15 01:51:59 +00002242void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002243 // What to do depends on the size of the integer and the size of the pointer.
2244 // We can either truncate, zero extend, or no-op, accordingly.
2245 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002246 EVT SrcVT = N.getValueType();
2247 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002248 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002249}
2250
Dan Gohman46510a72010-04-15 01:51:59 +00002251void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252 // What to do depends on the size of the integer and the size of the pointer.
2253 // We can either truncate, zero extend, or no-op, accordingly.
2254 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002255 EVT SrcVT = N.getValueType();
2256 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002257 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002258}
2259
Dan Gohman46510a72010-04-15 01:51:59 +00002260void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002261 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002262 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002263
Bill Wendling49fcff82009-12-21 22:30:11 +00002264 // BitCast assures us that source and destination are the same size so this is
2265 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002266 if (DestVT != N.getValueType())
2267 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2268 DestVT, N)); // convert types.
2269 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002270 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002271}
2272
Dan Gohman46510a72010-04-15 01:51:59 +00002273void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274 SDValue InVec = getValue(I.getOperand(0));
2275 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002276 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002277 TLI.getPointerTy(),
2278 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002279 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2280 TLI.getValueType(I.getType()),
2281 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002282}
2283
Dan Gohman46510a72010-04-15 01:51:59 +00002284void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002285 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002286 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002287 TLI.getPointerTy(),
2288 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002289 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2290 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002291}
2292
Mon P Wangaeb06d22008-11-10 04:46:22 +00002293// Utility for visitShuffleVector - Returns true if the mask is mask starting
2294// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002295static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2296 unsigned MaskNumElts = Mask.size();
2297 for (unsigned i = 0; i != MaskNumElts; ++i)
2298 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002299 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002300 return true;
2301}
2302
Dan Gohman46510a72010-04-15 01:51:59 +00002303void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002304 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002305 SDValue Src1 = getValue(I.getOperand(0));
2306 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307
Nate Begeman9008ca62009-04-27 18:41:29 +00002308 // Convert the ConstantVector mask operand into an array of ints, with -1
2309 // representing undef values.
2310 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002311 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002312 unsigned MaskNumElts = MaskElts.size();
2313 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002314 if (isa<UndefValue>(MaskElts[i]))
2315 Mask.push_back(-1);
2316 else
2317 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2318 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002319
Owen Andersone50ed302009-08-10 22:56:29 +00002320 EVT VT = TLI.getValueType(I.getType());
2321 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002322 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002323
Mon P Wangc7849c22008-11-16 05:06:27 +00002324 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002325 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2326 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002327 return;
2328 }
2329
2330 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002331 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2332 // Mask is longer than the source vectors and is a multiple of the source
2333 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002334 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002335 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2336 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002337 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2338 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002339 return;
2340 }
2341
Mon P Wangc7849c22008-11-16 05:06:27 +00002342 // Pad both vectors with undefs to make them the same length as the mask.
2343 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002344 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2345 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002346 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002347
Nate Begeman9008ca62009-04-27 18:41:29 +00002348 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2349 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002350 MOps1[0] = Src1;
2351 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002352
2353 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2354 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002355 &MOps1[0], NumConcat);
2356 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002357 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002358 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002359
Mon P Wangaeb06d22008-11-10 04:46:22 +00002360 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002361 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002362 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002363 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002364 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002365 MappedOps.push_back(Idx);
2366 else
2367 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002368 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002369
Bill Wendling4533cac2010-01-28 21:51:40 +00002370 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2371 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002372 return;
2373 }
2374
Mon P Wangc7849c22008-11-16 05:06:27 +00002375 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002376 // Analyze the access pattern of the vector to see if we can extract
2377 // two subvectors and do the shuffle. The analysis is done by calculating
2378 // the range of elements the mask access on both vectors.
2379 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2380 int MaxRange[2] = {-1, -1};
2381
Nate Begeman5a5ca152009-04-29 05:20:52 +00002382 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002383 int Idx = Mask[i];
2384 int Input = 0;
2385 if (Idx < 0)
2386 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002387
Nate Begeman5a5ca152009-04-29 05:20:52 +00002388 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002389 Input = 1;
2390 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002391 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002392 if (Idx > MaxRange[Input])
2393 MaxRange[Input] = Idx;
2394 if (Idx < MinRange[Input])
2395 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002396 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002397
Mon P Wangc7849c22008-11-16 05:06:27 +00002398 // Check if the access is smaller than the vector size and can we find
2399 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002400 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2401 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002402 int StartIdx[2]; // StartIdx to extract from
2403 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002404 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002405 RangeUse[Input] = 0; // Unused
2406 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002407 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002408 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002409 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002410 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002411 RangeUse[Input] = 1; // Extract from beginning of the vector
2412 StartIdx[Input] = 0;
2413 } else {
2414 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002415 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002416 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002417 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002418 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002419 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002420 }
2421
Bill Wendling636e2582009-08-21 18:16:06 +00002422 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002423 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002424 return;
2425 }
2426 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2427 // Extract appropriate subvector and generate a vector shuffle
2428 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002429 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002430 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002431 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002432 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002433 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002434 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002435 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002436
Mon P Wangc7849c22008-11-16 05:06:27 +00002437 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002438 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002439 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002440 int Idx = Mask[i];
2441 if (Idx < 0)
2442 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002443 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002444 MappedOps.push_back(Idx - StartIdx[0]);
2445 else
2446 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002447 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002448
Bill Wendling4533cac2010-01-28 21:51:40 +00002449 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2450 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002451 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002452 }
2453 }
2454
Mon P Wangc7849c22008-11-16 05:06:27 +00002455 // We can't use either concat vectors or extract subvectors so fall back to
2456 // replacing the shuffle with extract and build vector.
2457 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002458 EVT EltVT = VT.getVectorElementType();
2459 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002460 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002461 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002462 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002463 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002464 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002465 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002466 SDValue Res;
2467
Nate Begeman5a5ca152009-04-29 05:20:52 +00002468 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002469 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2470 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002471 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002472 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2473 EltVT, Src2,
2474 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2475
2476 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002477 }
2478 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002479
Bill Wendling4533cac2010-01-28 21:51:40 +00002480 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2481 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002482}
2483
Dan Gohman46510a72010-04-15 01:51:59 +00002484void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002485 const Value *Op0 = I.getOperand(0);
2486 const Value *Op1 = I.getOperand(1);
2487 const Type *AggTy = I.getType();
2488 const Type *ValTy = Op1->getType();
2489 bool IntoUndef = isa<UndefValue>(Op0);
2490 bool FromUndef = isa<UndefValue>(Op1);
2491
2492 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2493 I.idx_begin(), I.idx_end());
2494
Owen Andersone50ed302009-08-10 22:56:29 +00002495 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002497 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002498 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2499
2500 unsigned NumAggValues = AggValueVTs.size();
2501 unsigned NumValValues = ValValueVTs.size();
2502 SmallVector<SDValue, 4> Values(NumAggValues);
2503
2504 SDValue Agg = getValue(Op0);
2505 SDValue Val = getValue(Op1);
2506 unsigned i = 0;
2507 // Copy the beginning value(s) from the original aggregate.
2508 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002509 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002510 SDValue(Agg.getNode(), Agg.getResNo() + i);
2511 // Copy values from the inserted value(s).
2512 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002513 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002514 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2515 // Copy remaining value(s) from the original aggregate.
2516 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002517 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002518 SDValue(Agg.getNode(), Agg.getResNo() + i);
2519
Bill Wendling4533cac2010-01-28 21:51:40 +00002520 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2521 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2522 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002523}
2524
Dan Gohman46510a72010-04-15 01:51:59 +00002525void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002526 const Value *Op0 = I.getOperand(0);
2527 const Type *AggTy = Op0->getType();
2528 const Type *ValTy = I.getType();
2529 bool OutOfUndef = isa<UndefValue>(Op0);
2530
2531 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2532 I.idx_begin(), I.idx_end());
2533
Owen Andersone50ed302009-08-10 22:56:29 +00002534 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002535 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2536
2537 unsigned NumValValues = ValValueVTs.size();
2538 SmallVector<SDValue, 4> Values(NumValValues);
2539
2540 SDValue Agg = getValue(Op0);
2541 // Copy out the selected value(s).
2542 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2543 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002544 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002545 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002546 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002547
Bill Wendling4533cac2010-01-28 21:51:40 +00002548 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2549 DAG.getVTList(&ValValueVTs[0], NumValValues),
2550 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002551}
2552
Dan Gohman46510a72010-04-15 01:51:59 +00002553void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002554 SDValue N = getValue(I.getOperand(0));
2555 const Type *Ty = I.getOperand(0)->getType();
2556
Dan Gohman46510a72010-04-15 01:51:59 +00002557 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002559 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002560 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2561 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2562 if (Field) {
2563 // N = N + Offset
2564 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002565 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002566 DAG.getIntPtrConstant(Offset));
2567 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002569 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002570 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2571 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2572
2573 // Offset canonically 0 for unions, but type changes
2574 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002575 } else {
2576 Ty = cast<SequentialType>(Ty)->getElementType();
2577
2578 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002579 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002580 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002581 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002582 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002583 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002584 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002585 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002586 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002587 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2588 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002589 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002590 else
Evan Chengb1032a82009-02-09 20:54:38 +00002591 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002592
Dale Johannesen66978ee2009-01-31 02:22:37 +00002593 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002594 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002595 continue;
2596 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002597
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002598 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002599 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2600 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002601 SDValue IdxN = getValue(Idx);
2602
2603 // If the index is smaller or larger than intptr_t, truncate or extend
2604 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002605 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002606
2607 // If this is a multiply by a power of two, turn it into a shl
2608 // immediately. This is a very common case.
2609 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002610 if (ElementSize.isPowerOf2()) {
2611 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002612 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002613 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002614 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002615 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002616 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002617 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002618 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002619 }
2620 }
2621
Scott Michelfdc40a02009-02-17 22:15:04 +00002622 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002623 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002624 }
2625 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002627 setValue(&I, N);
2628}
2629
Dan Gohman46510a72010-04-15 01:51:59 +00002630void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002631 // If this is a fixed sized alloca in the entry block of the function,
2632 // allocate it statically on the stack.
2633 if (FuncInfo.StaticAllocaMap.count(&I))
2634 return; // getValue will auto-populate this.
2635
2636 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002637 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002638 unsigned Align =
2639 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2640 I.getAlignment());
2641
2642 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002643
Chris Lattner0b18e592009-03-17 19:36:00 +00002644 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2645 AllocSize,
2646 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002647
Owen Andersone50ed302009-08-10 22:56:29 +00002648 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002649 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002650
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002651 // Handle alignment. If the requested alignment is less than or equal to
2652 // the stack alignment, ignore it. If the size is greater than or equal to
2653 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002654 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002655 if (Align <= StackAlign)
2656 Align = 0;
2657
2658 // Round the size of the allocation up to the stack alignment size
2659 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002660 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002661 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002662 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002664 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002665 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002666 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002667 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2668
2669 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002671 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002672 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002673 setValue(&I, DSA);
2674 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002675
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002676 // Inform the Frame Information that we have just allocated a variable-sized
2677 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002678 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002679}
2680
Dan Gohman46510a72010-04-15 01:51:59 +00002681void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682 const Value *SV = I.getOperand(0);
2683 SDValue Ptr = getValue(SV);
2684
2685 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002686
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002687 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002688 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002689 unsigned Alignment = I.getAlignment();
2690
Owen Andersone50ed302009-08-10 22:56:29 +00002691 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002692 SmallVector<uint64_t, 4> Offsets;
2693 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2694 unsigned NumValues = ValueVTs.size();
2695 if (NumValues == 0)
2696 return;
2697
2698 SDValue Root;
2699 bool ConstantMemory = false;
2700 if (I.isVolatile())
2701 // Serialize volatile loads with other side effects.
2702 Root = getRoot();
2703 else if (AA->pointsToConstantMemory(SV)) {
2704 // Do not serialize (non-volatile) loads of constant memory with anything.
2705 Root = DAG.getEntryNode();
2706 ConstantMemory = true;
2707 } else {
2708 // Do not serialize non-volatile loads against each other.
2709 Root = DAG.getRoot();
2710 }
2711
2712 SmallVector<SDValue, 4> Values(NumValues);
2713 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002714 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002715 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002716 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2717 PtrVT, Ptr,
2718 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002719 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002720 A, SV, Offsets[i], isVolatile,
2721 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002722
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002723 Values[i] = L;
2724 Chains[i] = L.getValue(1);
2725 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002726
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002728 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002729 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002730 if (isVolatile)
2731 DAG.setRoot(Chain);
2732 else
2733 PendingLoads.push_back(Chain);
2734 }
2735
Bill Wendling4533cac2010-01-28 21:51:40 +00002736 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2737 DAG.getVTList(&ValueVTs[0], NumValues),
2738 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002739}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002740
Dan Gohman46510a72010-04-15 01:51:59 +00002741void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2742 const Value *SrcV = I.getOperand(0);
2743 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002744
Owen Andersone50ed302009-08-10 22:56:29 +00002745 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002746 SmallVector<uint64_t, 4> Offsets;
2747 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2748 unsigned NumValues = ValueVTs.size();
2749 if (NumValues == 0)
2750 return;
2751
2752 // Get the lowered operands. Note that we do this after
2753 // checking if NumResults is zero, because with zero results
2754 // the operands won't have values in the map.
2755 SDValue Src = getValue(SrcV);
2756 SDValue Ptr = getValue(PtrV);
2757
2758 SDValue Root = getRoot();
2759 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002760 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002761 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002762 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002763 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002764
2765 for (unsigned i = 0; i != NumValues; ++i) {
2766 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2767 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002768 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002769 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002770 Add, PtrV, Offsets[i], isVolatile,
2771 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002772 }
2773
Bill Wendling4533cac2010-01-28 21:51:40 +00002774 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2775 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776}
2777
2778/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2779/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002780void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002781 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782 bool HasChain = !I.doesNotAccessMemory();
2783 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2784
2785 // Build the operand list.
2786 SmallVector<SDValue, 8> Ops;
2787 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2788 if (OnlyLoad) {
2789 // We don't need to serialize loads against other loads.
2790 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002791 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 Ops.push_back(getRoot());
2793 }
2794 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002795
2796 // Info is set by getTgtMemInstrinsic
2797 TargetLowering::IntrinsicInfo Info;
2798 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2799
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002800 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002801 if (!IsTgtIntrinsic)
2802 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002803
2804 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002805 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806 SDValue Op = getValue(I.getOperand(i));
2807 assert(TLI.isTypeLegal(Op.getValueType()) &&
2808 "Intrinsic uses a non-legal type?");
2809 Ops.push_back(Op);
2810 }
2811
Owen Andersone50ed302009-08-10 22:56:29 +00002812 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002813 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2814#ifndef NDEBUG
2815 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2816 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2817 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002818 }
Bob Wilson8d919552009-07-31 22:41:21 +00002819#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002822 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002823
Bob Wilson8d919552009-07-31 22:41:21 +00002824 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825
2826 // Create the node.
2827 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002828 if (IsTgtIntrinsic) {
2829 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002830 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002831 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002832 Info.memVT, Info.ptrVal, Info.offset,
2833 Info.align, Info.vol,
2834 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002835 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002836 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002837 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002838 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002839 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002840 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002841 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002842 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002843 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002844 }
2845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846 if (HasChain) {
2847 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2848 if (OnlyLoad)
2849 PendingLoads.push_back(Chain);
2850 else
2851 DAG.setRoot(Chain);
2852 }
Bill Wendling856ff412009-12-22 00:12:37 +00002853
Benjamin Kramerf0127052010-01-05 13:12:22 +00002854 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002855 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002856 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002857 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002858 }
Bill Wendling856ff412009-12-22 00:12:37 +00002859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002860 setValue(&I, Result);
2861 }
2862}
2863
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002864/// GetSignificand - Get the significand and build it into a floating-point
2865/// number with exponent of 1:
2866///
2867/// Op = (Op & 0x007fffff) | 0x3f800000;
2868///
2869/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002870static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002871GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2873 DAG.getConstant(0x007fffff, MVT::i32));
2874 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2875 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002876 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002877}
2878
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002879/// GetExponent - Get the exponent:
2880///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002881/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002882///
2883/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002884static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002885GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002886 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002887 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2888 DAG.getConstant(0x7f800000, MVT::i32));
2889 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002890 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2892 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002893 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002894}
2895
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002896/// getF32Constant - Get 32-bit floating point constant.
2897static SDValue
2898getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002899 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002900}
2901
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002902/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002903/// visitIntrinsicCall: I is a call instruction
2904/// Op is the associated NodeType for I
2905const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002906SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2907 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002908 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002909 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002910 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002911 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002912 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002913 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002914 getValue(I.getOperand(2)),
2915 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002916 setValue(&I, L);
2917 DAG.setRoot(L.getValue(1));
2918 return 0;
2919}
2920
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002921// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002922const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002923SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002924 SDValue Op1 = getValue(I.getOperand(1));
2925 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002926
Owen Anderson825b72b2009-08-11 20:47:22 +00002927 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002928 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002929 return 0;
2930}
Bill Wendling74c37652008-12-09 22:08:41 +00002931
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002932/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2933/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002934void
Dan Gohman46510a72010-04-15 01:51:59 +00002935SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002936 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002937 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002938
Eric Christopher551754c2010-04-16 23:37:20 +00002939 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002940 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002941 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002942
2943 // Put the exponent in the right bit position for later addition to the
2944 // final result:
2945 //
2946 // #define LOG2OFe 1.4426950f
2947 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002948 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002949 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002950 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002951
2952 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2954 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002955
2956 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002958 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002959
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002960 if (LimitFloatPrecision <= 6) {
2961 // For floating-point precision of 6:
2962 //
2963 // TwoToFractionalPartOfX =
2964 // 0.997535578f +
2965 // (0.735607626f + 0.252464424f * x) * x;
2966 //
2967 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002968 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002969 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002970 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002971 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002972 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2973 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002974 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002975 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002976
2977 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002979 TwoToFracPartOfX, IntegerPartOfX);
2980
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002982 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2983 // For floating-point precision of 12:
2984 //
2985 // TwoToFractionalPartOfX =
2986 // 0.999892986f +
2987 // (0.696457318f +
2988 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2989 //
2990 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002991 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002992 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002993 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002994 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2996 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002997 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2999 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003000 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003001 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003002
3003 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003004 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003005 TwoToFracPartOfX, IntegerPartOfX);
3006
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003008 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3009 // For floating-point precision of 18:
3010 //
3011 // TwoToFractionalPartOfX =
3012 // 0.999999982f +
3013 // (0.693148872f +
3014 // (0.240227044f +
3015 // (0.554906021e-1f +
3016 // (0.961591928e-2f +
3017 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3018 //
3019 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003020 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003021 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003022 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003023 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3025 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003026 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003027 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3028 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003029 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003030 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3031 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003032 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003033 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3034 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003035 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3037 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003038 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003039 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003041
3042 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003043 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003044 TwoToFracPartOfX, IntegerPartOfX);
3045
Owen Anderson825b72b2009-08-11 20:47:22 +00003046 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003047 }
3048 } else {
3049 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003050 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003051 getValue(I.getOperand(1)).getValueType(),
3052 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003053 }
3054
Dale Johannesen59e577f2008-09-05 18:38:42 +00003055 setValue(&I, result);
3056}
3057
Bill Wendling39150252008-09-09 20:39:27 +00003058/// visitLog - Lower a log intrinsic. Handles the special sequences for
3059/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003060void
Dan Gohman46510a72010-04-15 01:51:59 +00003061SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003062 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003063 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003064
Eric Christopher551754c2010-04-16 23:37:20 +00003065 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003066 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003067 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003069
3070 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003071 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003073 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003074
3075 // Get the significand and build it into a floating-point number with
3076 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003077 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003078
3079 if (LimitFloatPrecision <= 6) {
3080 // For floating-point precision of 6:
3081 //
3082 // LogofMantissa =
3083 // -1.1609546f +
3084 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003085 //
Bill Wendling39150252008-09-09 20:39:27 +00003086 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003087 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003088 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003090 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3092 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003093 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003094
Scott Michelfdc40a02009-02-17 22:15:04 +00003095 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003096 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003097 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3098 // For floating-point precision of 12:
3099 //
3100 // LogOfMantissa =
3101 // -1.7417939f +
3102 // (2.8212026f +
3103 // (-1.4699568f +
3104 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3105 //
3106 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003108 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003110 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3112 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003113 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003114 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3115 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003116 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003117 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3118 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003119 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003120
Scott Michelfdc40a02009-02-17 22:15:04 +00003121 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003122 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003123 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3124 // For floating-point precision of 18:
3125 //
3126 // LogOfMantissa =
3127 // -2.1072184f +
3128 // (4.2372794f +
3129 // (-3.7029485f +
3130 // (2.2781945f +
3131 // (-0.87823314f +
3132 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3133 //
3134 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003136 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003138 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3140 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003141 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003142 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3143 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003144 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003145 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3146 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003147 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3149 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003150 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3152 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003153 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003154
Scott Michelfdc40a02009-02-17 22:15:04 +00003155 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003156 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003157 }
3158 } else {
3159 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003160 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003161 getValue(I.getOperand(1)).getValueType(),
3162 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003163 }
3164
Dale Johannesen59e577f2008-09-05 18:38:42 +00003165 setValue(&I, result);
3166}
3167
Bill Wendling3eb59402008-09-09 00:28:24 +00003168/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3169/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003170void
Dan Gohman46510a72010-04-15 01:51:59 +00003171SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003172 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003173 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003174
Eric Christopher551754c2010-04-16 23:37:20 +00003175 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003176 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003177 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003178 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003179
Bill Wendling39150252008-09-09 20:39:27 +00003180 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003181 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003182
Bill Wendling3eb59402008-09-09 00:28:24 +00003183 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003184 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003185 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003186
Bill Wendling3eb59402008-09-09 00:28:24 +00003187 // Different possible minimax approximations of significand in
3188 // floating-point for various degrees of accuracy over [1,2].
3189 if (LimitFloatPrecision <= 6) {
3190 // For floating-point precision of 6:
3191 //
3192 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3193 //
3194 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003196 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003198 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3200 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003201 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003202
Scott Michelfdc40a02009-02-17 22:15:04 +00003203 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003205 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3206 // For floating-point precision of 12:
3207 //
3208 // Log2ofMantissa =
3209 // -2.51285454f +
3210 // (4.07009056f +
3211 // (-2.12067489f +
3212 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003213 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003214 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003215 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003216 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003218 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3220 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003221 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3223 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003224 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3226 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003228
Scott Michelfdc40a02009-02-17 22:15:04 +00003229 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003230 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003231 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3232 // For floating-point precision of 18:
3233 //
3234 // Log2ofMantissa =
3235 // -3.0400495f +
3236 // (6.1129976f +
3237 // (-5.3420409f +
3238 // (3.2865683f +
3239 // (-1.2669343f +
3240 // (0.27515199f -
3241 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3242 //
3243 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003247 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3249 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003251 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3252 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003253 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3255 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003256 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3258 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003259 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3261 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003262 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003263
Scott Michelfdc40a02009-02-17 22:15:04 +00003264 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003266 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003267 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003268 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003269 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003270 getValue(I.getOperand(1)).getValueType(),
3271 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003272 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003273
Dale Johannesen59e577f2008-09-05 18:38:42 +00003274 setValue(&I, result);
3275}
3276
Bill Wendling3eb59402008-09-09 00:28:24 +00003277/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3278/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003279void
Dan Gohman46510a72010-04-15 01:51:59 +00003280SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003281 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003282 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003283
Eric Christopher551754c2010-04-16 23:37:20 +00003284 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003285 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003286 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003288
Bill Wendling39150252008-09-09 20:39:27 +00003289 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003290 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003292 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003293
3294 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003295 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003296 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003297
3298 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003299 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003300 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003301 // Log10ofMantissa =
3302 // -0.50419619f +
3303 // (0.60948995f - 0.10380950f * x) * x;
3304 //
3305 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003306 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003307 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003308 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003309 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3311 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003312 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003313
Scott Michelfdc40a02009-02-17 22:15:04 +00003314 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003316 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3317 // For floating-point precision of 12:
3318 //
3319 // Log10ofMantissa =
3320 // -0.64831180f +
3321 // (0.91751397f +
3322 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3323 //
3324 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003326 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003328 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3330 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003331 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3333 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003334 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003335
Scott Michelfdc40a02009-02-17 22:15:04 +00003336 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003338 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003339 // For floating-point precision of 18:
3340 //
3341 // Log10ofMantissa =
3342 // -0.84299375f +
3343 // (1.5327582f +
3344 // (-1.0688956f +
3345 // (0.49102474f +
3346 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3347 //
3348 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003350 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003351 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3354 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3357 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003358 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3360 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3363 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003365
Scott Michelfdc40a02009-02-17 22:15:04 +00003366 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003368 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003369 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003370 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003371 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003372 getValue(I.getOperand(1)).getValueType(),
3373 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003374 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003375
Dale Johannesen59e577f2008-09-05 18:38:42 +00003376 setValue(&I, result);
3377}
3378
Bill Wendlinge10c8142008-09-09 22:39:21 +00003379/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3380/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003381void
Dan Gohman46510a72010-04-15 01:51:59 +00003382SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003383 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003384 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003385
Eric Christopher551754c2010-04-16 23:37:20 +00003386 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003387 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003388 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003389
Owen Anderson825b72b2009-08-11 20:47:22 +00003390 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003391
3392 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3394 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003395
3396 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003398 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003399
3400 if (LimitFloatPrecision <= 6) {
3401 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003402 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003403 // TwoToFractionalPartOfX =
3404 // 0.997535578f +
3405 // (0.735607626f + 0.252464424f * x) * x;
3406 //
3407 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003409 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003410 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003411 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3413 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003414 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003416 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003418
Scott Michelfdc40a02009-02-17 22:15:04 +00003419 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003421 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3422 // For floating-point precision of 12:
3423 //
3424 // TwoToFractionalPartOfX =
3425 // 0.999892986f +
3426 // (0.696457318f +
3427 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3428 //
3429 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003430 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003431 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003433 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3435 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003436 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3438 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003439 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003441 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003442 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003443
Scott Michelfdc40a02009-02-17 22:15:04 +00003444 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003446 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3447 // For floating-point precision of 18:
3448 //
3449 // TwoToFractionalPartOfX =
3450 // 0.999999982f +
3451 // (0.693148872f +
3452 // (0.240227044f +
3453 // (0.554906021e-1f +
3454 // (0.961591928e-2f +
3455 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3456 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003457 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003458 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3462 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003463 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3465 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003466 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3468 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003469 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3471 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003472 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3474 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003475 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003477 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003478 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003479
Scott Michelfdc40a02009-02-17 22:15:04 +00003480 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003482 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003483 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003484 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003485 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003486 getValue(I.getOperand(1)).getValueType(),
3487 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003488 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003489
Dale Johannesen601d3c02008-09-05 01:48:15 +00003490 setValue(&I, result);
3491}
3492
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003493/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3494/// limited-precision mode with x == 10.0f.
3495void
Dan Gohman46510a72010-04-15 01:51:59 +00003496SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003497 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003498 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003499 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003500 bool IsExp10 = false;
3501
Owen Anderson825b72b2009-08-11 20:47:22 +00003502 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003503 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003504 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3505 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3506 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3507 APFloat Ten(10.0f);
3508 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3509 }
3510 }
3511 }
3512
3513 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003514 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003515
3516 // Put the exponent in the right bit position for later addition to the
3517 // final result:
3518 //
3519 // #define LOG2OF10 3.3219281f
3520 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003521 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003522 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003524
3525 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3527 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003528
3529 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003531 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003532
3533 if (LimitFloatPrecision <= 6) {
3534 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003535 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003536 // twoToFractionalPartOfX =
3537 // 0.997535578f +
3538 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003539 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003540 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003542 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003544 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3546 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003547 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003549 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003551
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003552 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003554 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3555 // For floating-point precision of 12:
3556 //
3557 // TwoToFractionalPartOfX =
3558 // 0.999892986f +
3559 // (0.696457318f +
3560 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3561 //
3562 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003564 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3568 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003569 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3571 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003572 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003574 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003576
Scott Michelfdc40a02009-02-17 22:15:04 +00003577 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003579 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3580 // For floating-point precision of 18:
3581 //
3582 // TwoToFractionalPartOfX =
3583 // 0.999999982f +
3584 // (0.693148872f +
3585 // (0.240227044f +
3586 // (0.554906021e-1f +
3587 // (0.961591928e-2f +
3588 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3589 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003590 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003591 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003593 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3595 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003596 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3598 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003599 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3601 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003602 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3604 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003605 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3607 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003608 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003610 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003611 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003612
Scott Michelfdc40a02009-02-17 22:15:04 +00003613 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003615 }
3616 } else {
3617 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003618 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003619 getValue(I.getOperand(1)).getValueType(),
3620 getValue(I.getOperand(1)),
3621 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003622 }
3623
3624 setValue(&I, result);
3625}
3626
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003627
3628/// ExpandPowI - Expand a llvm.powi intrinsic.
3629static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3630 SelectionDAG &DAG) {
3631 // If RHS is a constant, we can expand this out to a multiplication tree,
3632 // otherwise we end up lowering to a call to __powidf2 (for example). When
3633 // optimizing for size, we only want to do this if the expansion would produce
3634 // a small number of multiplies, otherwise we do the full expansion.
3635 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3636 // Get the exponent as a positive value.
3637 unsigned Val = RHSC->getSExtValue();
3638 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003639
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003640 // powi(x, 0) -> 1.0
3641 if (Val == 0)
3642 return DAG.getConstantFP(1.0, LHS.getValueType());
3643
Dan Gohmanae541aa2010-04-15 04:33:49 +00003644 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003645 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3646 // If optimizing for size, don't insert too many multiplies. This
3647 // inserts up to 5 multiplies.
3648 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3649 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003650 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003651 // powi(x,15) generates one more multiply than it should), but this has
3652 // the benefit of being both really simple and much better than a libcall.
3653 SDValue Res; // Logically starts equal to 1.0
3654 SDValue CurSquare = LHS;
3655 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003656 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003657 if (Res.getNode())
3658 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3659 else
3660 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003661 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003662
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003663 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3664 CurSquare, CurSquare);
3665 Val >>= 1;
3666 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003667
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003668 // If the original was negative, invert the result, producing 1/(x*x*x).
3669 if (RHSC->getSExtValue() < 0)
3670 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3671 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3672 return Res;
3673 }
3674 }
3675
3676 // Otherwise, expand to a libcall.
3677 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3678}
3679
3680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003681/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3682/// we want to emit this as a call to a named external function, return the name
3683/// otherwise lower it and return null.
3684const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003685SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003686 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003687 SDValue Res;
3688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003689 switch (Intrinsic) {
3690 default:
3691 // By default, turn this into a target intrinsic node.
3692 visitTargetIntrinsic(I, Intrinsic);
3693 return 0;
3694 case Intrinsic::vastart: visitVAStart(I); return 0;
3695 case Intrinsic::vaend: visitVAEnd(I); return 0;
3696 case Intrinsic::vacopy: visitVACopy(I); return 0;
3697 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003698 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003699 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003700 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003701 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003702 setValue(&I, DAG.getNode(ISD::FRAMEADDR, 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;
3705 case Intrinsic::setjmp:
3706 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003707 case Intrinsic::longjmp:
3708 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003709 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003710 // Assert for address < 256 since we support only user defined address
3711 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003712 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003713 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003714 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003715 < 256 &&
3716 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003717 SDValue Op1 = getValue(I.getOperand(1));
3718 SDValue Op2 = getValue(I.getOperand(2));
3719 SDValue Op3 = getValue(I.getOperand(3));
3720 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3721 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003722 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003723 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003724 return 0;
3725 }
Chris Lattner824b9582008-11-21 16:42:48 +00003726 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003727 // Assert for address < 256 since we support only user defined address
3728 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003729 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003730 < 256 &&
3731 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003732 SDValue Op1 = getValue(I.getOperand(1));
3733 SDValue Op2 = getValue(I.getOperand(2));
3734 SDValue Op3 = getValue(I.getOperand(3));
3735 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3736 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003737 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003738 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003739 return 0;
3740 }
Chris Lattner824b9582008-11-21 16:42:48 +00003741 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003742 // Assert for address < 256 since we support only user defined address
3743 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003744 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003745 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003746 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003747 < 256 &&
3748 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003749 SDValue Op1 = getValue(I.getOperand(1));
3750 SDValue Op2 = getValue(I.getOperand(2));
3751 SDValue Op3 = getValue(I.getOperand(3));
3752 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3753 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003754
3755 // If the source and destination are known to not be aliases, we can
3756 // lower memmove as memcpy.
3757 uint64_t Size = -1ULL;
3758 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003759 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003760 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003761 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003762 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003763 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003764 return 0;
3765 }
3766
Mon P Wang20adc9d2010-04-04 03:10:48 +00003767 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003768 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003769 return 0;
3770 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003771 case Intrinsic::dbg_declare: {
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003772 // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None.
3773 // The real handling of this intrinsic is in FastISel.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003774 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00003775 // FIXME: Variable debug info is not supported here.
3776 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003777 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003778 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003779 return 0;
3780
Devang Patelac1ceb32009-10-09 22:42:28 +00003781 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00003782 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003783 if (!Address)
3784 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003785 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003786 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003787 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel24f20e02009-08-22 17:12:53 +00003788 // Don't handle byval struct arguments or VLAs, for example.
3789 if (!AI)
3790 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003791 DenseMap<const AllocaInst*, int>::iterator SI =
3792 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003793 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00003794 return 0; // VLAs.
3795 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003796
Chris Lattner512063d2010-04-05 06:19:28 +00003797 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3798 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3799 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003800 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003801 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003802 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003803 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003804 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3805 return 0;
3806
3807 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003808 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003809 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003810 if (!V)
3811 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003812
3813 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3814 // but do not always have a corresponding SDNode built. The SDNodeOrder
3815 // absolute, but not relative, values are different depending on whether
3816 // debug info exists.
3817 ++SDNodeOrder;
3818 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Evan Cheng31441b72010-03-29 20:48:30 +00003819 DAG.AddDbgValue(DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003820 } else {
3821 SDValue &N = NodeMap[V];
Evan Cheng31441b72010-03-29 20:48:30 +00003822 if (N.getNode())
3823 DAG.AddDbgValue(DAG.getDbgValue(Variable, N.getNode(),
3824 N.getResNo(), Offset, dl, SDNodeOrder),
3825 N.getNode());
3826 else
Devang Patel00190342010-03-15 19:15:44 +00003827 // We may expand this to cover more cases. One case where we have no
3828 // data available is an unreferenced parameter; we need this fallback.
Evan Cheng31441b72010-03-29 20:48:30 +00003829 DAG.AddDbgValue(DAG.getDbgValue(Variable,
Devang Patel00190342010-03-15 19:15:44 +00003830 UndefValue::get(V->getType()),
Evan Cheng31441b72010-03-29 20:48:30 +00003831 Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003832 }
3833
3834 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003835 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003836 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003837 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003838 // Don't handle byval struct arguments or VLAs, for example.
3839 if (!AI)
3840 return 0;
3841 DenseMap<const AllocaInst*, int>::iterator SI =
3842 FuncInfo.StaticAllocaMap.find(AI);
3843 if (SI == FuncInfo.StaticAllocaMap.end())
3844 return 0; // VLAs.
3845 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003846
Chris Lattner512063d2010-04-05 06:19:28 +00003847 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3848 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3849 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003850 return 0;
3851 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003852 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003853 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003854 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3855 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003857 SDValue Ops[1];
3858 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003859 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003860 setValue(&I, Op);
3861 DAG.setRoot(Op.getValue(1));
3862 return 0;
3863 }
3864
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003865 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003866 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003867 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003868 if (CallMBB->isLandingPad())
3869 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003870 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003871#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003872 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003873#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003874 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3875 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003876 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003877 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003878
Chris Lattner3a5815f2009-09-17 23:54:54 +00003879 // Insert the EHSELECTION instruction.
3880 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3881 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003882 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003883 Ops[1] = getRoot();
3884 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003885 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003886 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003887 return 0;
3888 }
3889
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003890 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003891 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003892 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003893 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3894 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003895 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003896 return 0;
3897 }
3898
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003899 case Intrinsic::eh_return_i32:
3900 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003901 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3902 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3903 MVT::Other,
3904 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003905 getValue(I.getOperand(1)),
3906 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003907 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003908 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003909 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003910 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003911 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003912 EVT VT = getValue(I.getOperand(1)).getValueType();
3913 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003914 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003915 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003916 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003917 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003918 TLI.getPointerTy()),
3919 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003920 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003921 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003922 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00003923 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
3924 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003925 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003926 }
Jim Grosbachca752c92010-01-28 01:45:32 +00003927 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00003928 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00003929 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00003930 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00003931 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00003932
Chris Lattner512063d2010-04-05 06:19:28 +00003933 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00003934 return 0;
3935 }
3936
Mon P Wang77cdf302008-11-10 20:54:11 +00003937 case Intrinsic::convertff:
3938 case Intrinsic::convertfsi:
3939 case Intrinsic::convertfui:
3940 case Intrinsic::convertsif:
3941 case Intrinsic::convertuif:
3942 case Intrinsic::convertss:
3943 case Intrinsic::convertsu:
3944 case Intrinsic::convertus:
3945 case Intrinsic::convertuu: {
3946 ISD::CvtCode Code = ISD::CVT_INVALID;
3947 switch (Intrinsic) {
3948 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3949 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3950 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3951 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3952 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3953 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3954 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3955 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3956 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3957 }
Owen Andersone50ed302009-08-10 22:56:29 +00003958 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00003959 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003960 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
3961 DAG.getValueType(DestVT),
3962 DAG.getValueType(getValue(Op1).getValueType()),
3963 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00003964 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003965 Code);
3966 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00003967 return 0;
3968 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003969 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00003970 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003971 getValue(I.getOperand(1)).getValueType(),
3972 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003973 return 0;
3974 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00003975 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3976 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003977 return 0;
3978 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00003979 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003980 getValue(I.getOperand(1)).getValueType(),
3981 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003982 return 0;
3983 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00003984 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003985 getValue(I.getOperand(1)).getValueType(),
3986 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003987 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003988 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003989 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003990 return 0;
3991 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003992 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003993 return 0;
3994 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003995 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003996 return 0;
3997 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003998 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003999 return 0;
4000 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004001 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004002 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004003 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004004 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004005 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004006 case Intrinsic::convert_to_fp16:
4007 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004008 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004009 return 0;
4010 case Intrinsic::convert_from_fp16:
4011 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004012 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004013 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004014 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004015 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004016 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004017 return 0;
4018 }
4019 case Intrinsic::readcyclecounter: {
4020 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004021 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4022 DAG.getVTList(MVT::i64, MVT::Other),
4023 &Op, 1);
4024 setValue(&I, Res);
4025 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004026 return 0;
4027 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004028 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004029 setValue(&I, DAG.getNode(ISD::BSWAP, 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::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004034 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004035 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004036 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004037 return 0;
4038 }
4039 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004040 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004041 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004042 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004043 return 0;
4044 }
4045 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004046 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004047 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004048 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004049 return 0;
4050 }
4051 case Intrinsic::stacksave: {
4052 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004053 Res = DAG.getNode(ISD::STACKSAVE, dl,
4054 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4055 setValue(&I, Res);
4056 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004057 return 0;
4058 }
4059 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004060 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004061 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004062 return 0;
4063 }
Bill Wendling57344502008-11-18 11:01:33 +00004064 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004065 // Emit code into the DAG to store the stack guard onto the stack.
4066 MachineFunction &MF = DAG.getMachineFunction();
4067 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004068 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004069
Eric Christopher551754c2010-04-16 23:37:20 +00004070 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4071 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004072
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004073 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004074 MFI->setStackProtectorIndex(FI);
4075
4076 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4077
4078 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004079 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4080 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004081 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004082 setValue(&I, Res);
4083 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004084 return 0;
4085 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004086 case Intrinsic::objectsize: {
4087 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004088 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004089
4090 assert(CI && "Non-constant type in __builtin_object_size?");
4091
Eric Christopher551754c2010-04-16 23:37:20 +00004092 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004093 EVT Ty = Arg.getValueType();
4094
Eric Christopherd060b252009-12-23 02:51:48 +00004095 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004096 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004097 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004098 Res = DAG.getConstant(0, Ty);
4099
4100 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004101 return 0;
4102 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004103 case Intrinsic::var_annotation:
4104 // Discard annotate attributes
4105 return 0;
4106
4107 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004108 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004109
4110 SDValue Ops[6];
4111 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004112 Ops[1] = getValue(I.getOperand(1));
4113 Ops[2] = getValue(I.getOperand(2));
4114 Ops[3] = getValue(I.getOperand(3));
4115 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004116 Ops[5] = DAG.getSrcValue(F);
4117
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004118 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4119 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4120 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004121
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004122 setValue(&I, Res);
4123 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004124 return 0;
4125 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004126 case Intrinsic::gcroot:
4127 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004128 const Value *Alloca = I.getOperand(1);
4129 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004130
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4132 GFI->addStackRoot(FI->getIndex(), TypeMap);
4133 }
4134 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004135 case Intrinsic::gcread:
4136 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004137 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004138 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004139 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004140 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004141 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004142 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004143 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004144 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004145 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004146 return implVisitAluOverflow(I, ISD::UADDO);
4147 case Intrinsic::sadd_with_overflow:
4148 return implVisitAluOverflow(I, ISD::SADDO);
4149 case Intrinsic::usub_with_overflow:
4150 return implVisitAluOverflow(I, ISD::USUBO);
4151 case Intrinsic::ssub_with_overflow:
4152 return implVisitAluOverflow(I, ISD::SSUBO);
4153 case Intrinsic::umul_with_overflow:
4154 return implVisitAluOverflow(I, ISD::UMULO);
4155 case Intrinsic::smul_with_overflow:
4156 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004158 case Intrinsic::prefetch: {
4159 SDValue Ops[4];
4160 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004161 Ops[1] = getValue(I.getOperand(1));
4162 Ops[2] = getValue(I.getOperand(2));
4163 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004164 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004165 return 0;
4166 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004168 case Intrinsic::memory_barrier: {
4169 SDValue Ops[6];
4170 Ops[0] = getRoot();
4171 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004172 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004173
Bill Wendling4533cac2010-01-28 21:51:40 +00004174 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004175 return 0;
4176 }
4177 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004178 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004179 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004180 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004181 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004182 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004183 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004184 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004185 getValue(I.getOperand(3)),
4186 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004187 setValue(&I, L);
4188 DAG.setRoot(L.getValue(1));
4189 return 0;
4190 }
4191 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004192 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004193 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004194 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004196 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004197 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004198 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004199 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004200 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004201 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004202 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004203 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004204 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004205 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004206 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004207 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004208 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004209 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004210 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004211 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004212 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004213
4214 case Intrinsic::invariant_start:
4215 case Intrinsic::lifetime_start:
4216 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004217 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004218 return 0;
4219 case Intrinsic::invariant_end:
4220 case Intrinsic::lifetime_end:
4221 // Discard region information.
4222 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004223 }
4224}
4225
Dan Gohman46510a72010-04-15 01:51:59 +00004226void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004227 bool isTailCall,
4228 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004229 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4230 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004231 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004232 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004233 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004234
4235 TargetLowering::ArgListTy Args;
4236 TargetLowering::ArgListEntry Entry;
4237 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004238
4239 // Check whether the function can return without sret-demotion.
4240 SmallVector<EVT, 4> OutVTs;
4241 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4242 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004243 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004244 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004245
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004246 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004247 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4248
4249 SDValue DemoteStackSlot;
4250
4251 if (!CanLowerReturn) {
4252 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4253 FTy->getReturnType());
4254 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4255 FTy->getReturnType());
4256 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004257 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004258 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4259
4260 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4261 Entry.Node = DemoteStackSlot;
4262 Entry.Ty = StackSlotPtrType;
4263 Entry.isSExt = false;
4264 Entry.isZExt = false;
4265 Entry.isInReg = false;
4266 Entry.isSRet = true;
4267 Entry.isNest = false;
4268 Entry.isByVal = false;
4269 Entry.Alignment = Align;
4270 Args.push_back(Entry);
4271 RetTy = Type::getVoidTy(FTy->getContext());
4272 }
4273
Dan Gohman46510a72010-04-15 01:51:59 +00004274 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004275 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004276 SDValue ArgNode = getValue(*i);
4277 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4278
4279 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004280 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4281 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4282 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4283 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4284 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4285 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004286 Entry.Alignment = CS.getParamAlignment(attrInd);
4287 Args.push_back(Entry);
4288 }
4289
Chris Lattner512063d2010-04-05 06:19:28 +00004290 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004291 // Insert a label before the invoke call to mark the try range. This can be
4292 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004293 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004294
Jim Grosbachca752c92010-01-28 01:45:32 +00004295 // For SjLj, keep track of which landing pads go with which invokes
4296 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004297 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004298 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004299 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004300 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004301 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004302 }
4303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004304 // Both PendingLoads and PendingExports must be flushed here;
4305 // this call might not return.
4306 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004307 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 }
4309
Dan Gohman98ca4f22009-08-05 01:29:28 +00004310 // Check if target-independent constraints permit a tail call here.
4311 // Target-dependent constraints are checked within TLI.LowerCallTo.
4312 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004313 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004314 isTailCall = false;
4315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004316 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004317 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004318 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004319 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004320 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004321 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004322 isTailCall,
4323 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004324 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004325 assert((isTailCall || Result.second.getNode()) &&
4326 "Non-null chain expected with non-tail call!");
4327 assert((Result.second.getNode() || !Result.first.getNode()) &&
4328 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004329 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004330 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004331 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004332 // The instruction result is the result of loading from the
4333 // hidden sret parameter.
4334 SmallVector<EVT, 1> PVTs;
4335 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4336
4337 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4338 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4339 EVT PtrVT = PVTs[0];
4340 unsigned NumValues = OutVTs.size();
4341 SmallVector<SDValue, 4> Values(NumValues);
4342 SmallVector<SDValue, 4> Chains(NumValues);
4343
4344 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004345 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4346 DemoteStackSlot,
4347 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004348 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004349 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004350 Values[i] = L;
4351 Chains[i] = L.getValue(1);
4352 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004353
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004354 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4355 MVT::Other, &Chains[0], NumValues);
4356 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004357
4358 // Collect the legal value parts into potentially illegal values
4359 // that correspond to the original function's return values.
4360 SmallVector<EVT, 4> RetTys;
4361 RetTy = FTy->getReturnType();
4362 ComputeValueVTs(TLI, RetTy, RetTys);
4363 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4364 SmallVector<SDValue, 4> ReturnValues;
4365 unsigned CurReg = 0;
4366 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4367 EVT VT = RetTys[I];
4368 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4369 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4370
4371 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004372 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004373 RegisterVT, VT, AssertOp);
4374 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004375 CurReg += NumRegs;
4376 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004377
Bill Wendling4533cac2010-01-28 21:51:40 +00004378 setValue(CS.getInstruction(),
4379 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4380 DAG.getVTList(&RetTys[0], RetTys.size()),
4381 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004382
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004383 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004384
4385 // As a special case, a null chain means that a tail call has been emitted and
4386 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004387 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004388 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004389 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004390 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391
Chris Lattner512063d2010-04-05 06:19:28 +00004392 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004393 // Insert a label at the end of the invoke call to mark the try range. This
4394 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004395 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004396 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397
4398 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004399 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 }
4401}
4402
Chris Lattner8047d9a2009-12-24 00:37:38 +00004403/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4404/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004405static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4406 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004407 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004408 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004409 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004410 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004411 if (C->isNullValue())
4412 continue;
4413 // Unknown instruction.
4414 return false;
4415 }
4416 return true;
4417}
4418
Dan Gohman46510a72010-04-15 01:51:59 +00004419static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4420 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004421 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004422
Chris Lattner8047d9a2009-12-24 00:37:38 +00004423 // Check to see if this load can be trivially constant folded, e.g. if the
4424 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004425 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004426 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004427 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004428 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004429
Dan Gohman46510a72010-04-15 01:51:59 +00004430 if (const Constant *LoadCst =
4431 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4432 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004433 return Builder.getValue(LoadCst);
4434 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004435
Chris Lattner8047d9a2009-12-24 00:37:38 +00004436 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4437 // still constant memory, the input chain can be the entry node.
4438 SDValue Root;
4439 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004440
Chris Lattner8047d9a2009-12-24 00:37:38 +00004441 // Do not serialize (non-volatile) loads of constant memory with anything.
4442 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4443 Root = Builder.DAG.getEntryNode();
4444 ConstantMemory = true;
4445 } else {
4446 // Do not serialize non-volatile loads against each other.
4447 Root = Builder.DAG.getRoot();
4448 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004449
Chris Lattner8047d9a2009-12-24 00:37:38 +00004450 SDValue Ptr = Builder.getValue(PtrVal);
4451 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4452 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004453 false /*volatile*/,
4454 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004455
Chris Lattner8047d9a2009-12-24 00:37:38 +00004456 if (!ConstantMemory)
4457 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4458 return LoadVal;
4459}
4460
4461
4462/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4463/// If so, return true and lower it, otherwise return false and it will be
4464/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004465bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004466 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4467 if (I.getNumOperands() != 4)
4468 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004469
Eric Christopher551754c2010-04-16 23:37:20 +00004470 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004471 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004472 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004473 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004474 return false;
4475
Eric Christopher551754c2010-04-16 23:37:20 +00004476 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004477
Chris Lattner8047d9a2009-12-24 00:37:38 +00004478 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4479 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004480 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4481 bool ActuallyDoIt = true;
4482 MVT LoadVT;
4483 const Type *LoadTy;
4484 switch (Size->getZExtValue()) {
4485 default:
4486 LoadVT = MVT::Other;
4487 LoadTy = 0;
4488 ActuallyDoIt = false;
4489 break;
4490 case 2:
4491 LoadVT = MVT::i16;
4492 LoadTy = Type::getInt16Ty(Size->getContext());
4493 break;
4494 case 4:
4495 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004496 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004497 break;
4498 case 8:
4499 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004500 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004501 break;
4502 /*
4503 case 16:
4504 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004505 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004506 LoadTy = VectorType::get(LoadTy, 4);
4507 break;
4508 */
4509 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004510
Chris Lattner04b091a2009-12-24 01:07:17 +00004511 // This turns into unaligned loads. We only do this if the target natively
4512 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4513 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004514
Chris Lattner04b091a2009-12-24 01:07:17 +00004515 // Require that we can find a legal MVT, and only do this if the target
4516 // supports unaligned loads of that type. Expanding into byte loads would
4517 // bloat the code.
4518 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4519 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4520 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4521 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4522 ActuallyDoIt = false;
4523 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004524
Chris Lattner04b091a2009-12-24 01:07:17 +00004525 if (ActuallyDoIt) {
4526 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4527 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004528
Chris Lattner04b091a2009-12-24 01:07:17 +00004529 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4530 ISD::SETNE);
4531 EVT CallVT = TLI.getValueType(I.getType(), true);
4532 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4533 return true;
4534 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004535 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004536
4537
Chris Lattner8047d9a2009-12-24 00:37:38 +00004538 return false;
4539}
4540
4541
Dan Gohman46510a72010-04-15 01:51:59 +00004542void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004543 const char *RenameFn = 0;
4544 if (Function *F = I.getCalledFunction()) {
4545 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004546 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004547 if (II) {
4548 if (unsigned IID = II->getIntrinsicID(F)) {
4549 RenameFn = visitIntrinsicCall(I, IID);
4550 if (!RenameFn)
4551 return;
4552 }
4553 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 if (unsigned IID = F->getIntrinsicID()) {
4555 RenameFn = visitIntrinsicCall(I, IID);
4556 if (!RenameFn)
4557 return;
4558 }
4559 }
4560
4561 // Check for well-known libc/libm calls. If the function is internal, it
4562 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004563 if (!F->hasLocalLinkage() && F->hasName()) {
4564 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004565 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004566 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004567 I.getOperand(1)->getType()->isFloatingPointTy() &&
4568 I.getType() == I.getOperand(1)->getType() &&
4569 I.getType() == I.getOperand(2)->getType()) {
4570 SDValue LHS = getValue(I.getOperand(1));
4571 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004572 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4573 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004574 return;
4575 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004576 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004577 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004578 I.getOperand(1)->getType()->isFloatingPointTy() &&
4579 I.getType() == I.getOperand(1)->getType()) {
4580 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004581 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4582 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004583 return;
4584 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004585 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004586 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004587 I.getOperand(1)->getType()->isFloatingPointTy() &&
4588 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004589 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004590 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004591 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4592 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004593 return;
4594 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004595 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004597 I.getOperand(1)->getType()->isFloatingPointTy() &&
4598 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004599 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004600 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004601 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4602 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004603 return;
4604 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004605 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4606 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004607 I.getOperand(1)->getType()->isFloatingPointTy() &&
4608 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004609 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004610 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004611 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4612 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004613 return;
4614 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004615 } else if (Name == "memcmp") {
4616 if (visitMemCmpCall(I))
4617 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004618 }
4619 }
Eric Christopher551754c2010-04-16 23:37:20 +00004620 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004621 visitInlineAsm(&I);
4622 return;
4623 }
4624
4625 SDValue Callee;
4626 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004627 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004628 else
Bill Wendling056292f2008-09-16 21:48:12 +00004629 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004630
Bill Wendling0d580132009-12-23 01:28:19 +00004631 // Check if we can potentially perform a tail call. More detailed checking is
4632 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004633 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634}
4635
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004636/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004637/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004638/// Chain/Flag as the input and updates them for the output Chain/Flag.
4639/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004640SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004641 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004642 // Assemble the legal parts into the final values.
4643 SmallVector<SDValue, 4> Values(ValueVTs.size());
4644 SmallVector<SDValue, 8> Parts;
4645 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4646 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004647 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004648 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004649 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650
4651 Parts.resize(NumRegs);
4652 for (unsigned i = 0; i != NumRegs; ++i) {
4653 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004654 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004655 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004656 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004657 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004658 *Flag = P.getValue(2);
4659 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004660
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004661 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004663 // If the source register was virtual and if we know something about it,
4664 // add an assert node.
4665 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4666 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4667 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4668 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4669 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4670 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004671
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 unsigned RegSize = RegisterVT.getSizeInBits();
4673 unsigned NumSignBits = LOI.NumSignBits;
4674 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004675
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 // FIXME: We capture more information than the dag can represent. For
4677 // now, just use the tightest assertzext/assertsext possible.
4678 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004679 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004680 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004681 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004682 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004683 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004685 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004686 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004687 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004690 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004691 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004692 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004693 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004694 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004695 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004696
Bill Wendling4533cac2010-01-28 21:51:40 +00004697 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004698 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004700 }
4701 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004702
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 Parts[i] = P;
4704 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004705
Bill Wendling46ada192010-03-02 01:55:18 +00004706 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004707 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004708 Part += NumRegs;
4709 Parts.clear();
4710 }
4711
Bill Wendling4533cac2010-01-28 21:51:40 +00004712 return DAG.getNode(ISD::MERGE_VALUES, dl,
4713 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4714 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715}
4716
4717/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004718/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719/// Chain/Flag as the input and updates them for the output Chain/Flag.
4720/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004721void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004722 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004723 // Get the list of the values's legal parts.
4724 unsigned NumRegs = Regs.size();
4725 SmallVector<SDValue, 8> Parts(NumRegs);
4726 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004727 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004728 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004729 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004730
Bill Wendling46ada192010-03-02 01:55:18 +00004731 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004732 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004733 &Parts[Part], NumParts, RegisterVT);
4734 Part += NumParts;
4735 }
4736
4737 // Copy the parts into the registers.
4738 SmallVector<SDValue, 8> Chains(NumRegs);
4739 for (unsigned i = 0; i != NumRegs; ++i) {
4740 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004741 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004742 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004743 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004744 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004745 *Flag = Part.getValue(1);
4746 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004748 Chains[i] = Part.getValue(0);
4749 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004750
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004751 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004752 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004753 // flagged to it. That is the CopyToReg nodes and the user are considered
4754 // a single scheduling unit. If we create a TokenFactor and return it as
4755 // chain, then the TokenFactor is both a predecessor (operand) of the
4756 // user as well as a successor (the TF operands are flagged to the user).
4757 // c1, f1 = CopyToReg
4758 // c2, f2 = CopyToReg
4759 // c3 = TokenFactor c1, c2
4760 // ...
4761 // = op c3, ..., f2
4762 Chain = Chains[NumRegs-1];
4763 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004764 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004765}
4766
4767/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004768/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004770void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4771 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004772 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004773 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004774 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004775 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004776 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004777 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004778 Ops.push_back(Res);
4779
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004780 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004781 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004782 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004783 for (unsigned i = 0; i != NumRegs; ++i) {
4784 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004785 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004786 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004787 }
4788}
4789
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004790/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004791/// i.e. it isn't a stack pointer or some other special register, return the
4792/// register class for the register. Otherwise, return null.
4793static const TargetRegisterClass *
4794isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4795 const TargetLowering &TLI,
4796 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004797 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 const TargetRegisterClass *FoundRC = 0;
4799 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4800 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802
4803 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004804 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4806 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4807 I != E; ++I) {
4808 if (TLI.isTypeLegal(*I)) {
4809 // If we have already found this register in a different register class,
4810 // choose the one with the largest VT specified. For example, on
4811 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813 ThisVT = *I;
4814 break;
4815 }
4816 }
4817 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004818
Owen Anderson825b72b2009-08-11 20:47:22 +00004819 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004821 // NOTE: This isn't ideal. In particular, this might allocate the
4822 // frame pointer in functions that need it (due to them not being taken
4823 // out of allocation, because a variable sized allocation hasn't been seen
4824 // yet). This is a slight code pessimization, but should still work.
4825 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4826 E = RC->allocation_order_end(MF); I != E; ++I)
4827 if (*I == Reg) {
4828 // We found a matching register class. Keep looking at others in case
4829 // we find one with larger registers that this physreg is also in.
4830 FoundRC = RC;
4831 FoundVT = ThisVT;
4832 break;
4833 }
4834 }
4835 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004836}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837
4838
4839namespace llvm {
4840/// AsmOperandInfo - This contains information for each constraint that we are
4841/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004842class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004843 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004844public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004845 /// CallOperand - If this is the result output operand or a clobber
4846 /// this is null, otherwise it is the incoming operand to the CallInst.
4847 /// This gets modified as the asm is processed.
4848 SDValue CallOperand;
4849
4850 /// AssignedRegs - If this is a register or register class operand, this
4851 /// contains the set of register corresponding to the operand.
4852 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004853
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4855 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4856 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004857
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004858 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4859 /// busy in OutputRegs/InputRegs.
4860 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004861 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004862 std::set<unsigned> &InputRegs,
4863 const TargetRegisterInfo &TRI) const {
4864 if (isOutReg) {
4865 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4866 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4867 }
4868 if (isInReg) {
4869 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4870 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4871 }
4872 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004873
Owen Andersone50ed302009-08-10 22:56:29 +00004874 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004875 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004877 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004878 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004879 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004880 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004881
Chris Lattner81249c92008-10-17 17:05:25 +00004882 if (isa<BasicBlock>(CallOperandVal))
4883 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004884
Chris Lattner81249c92008-10-17 17:05:25 +00004885 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004886
Chris Lattner81249c92008-10-17 17:05:25 +00004887 // If this is an indirect operand, the operand is a pointer to the
4888 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004889 if (isIndirect) {
4890 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4891 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004892 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004893 OpTy = PtrTy->getElementType();
4894 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004895
Chris Lattner81249c92008-10-17 17:05:25 +00004896 // If OpTy is not a single value, it may be a struct/union that we
4897 // can tile with integers.
4898 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4899 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4900 switch (BitSize) {
4901 default: break;
4902 case 1:
4903 case 8:
4904 case 16:
4905 case 32:
4906 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004907 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004908 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004909 break;
4910 }
4911 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004912
Chris Lattner81249c92008-10-17 17:05:25 +00004913 return TLI.getValueType(OpTy, true);
4914 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004915
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004916private:
4917 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4918 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004919 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004920 const TargetRegisterInfo &TRI) {
4921 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4922 Regs.insert(Reg);
4923 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4924 for (; *Aliases; ++Aliases)
4925 Regs.insert(*Aliases);
4926 }
4927};
4928} // end llvm namespace.
4929
4930
4931/// GetRegistersForValue - Assign registers (virtual or physical) for the
4932/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004933/// register allocator to handle the assignment process. However, if the asm
4934/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935/// allocation. This produces generally horrible, but correct, code.
4936///
4937/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004938/// Input and OutputRegs are the set of already allocated physical registers.
4939///
Dan Gohman2048b852009-11-23 18:04:58 +00004940void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004941GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004942 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004943 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004944 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 // Compute whether this value requires an input register, an output register,
4947 // or both.
4948 bool isOutReg = false;
4949 bool isInReg = false;
4950 switch (OpInfo.Type) {
4951 case InlineAsm::isOutput:
4952 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004953
4954 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004955 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004956 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004957 break;
4958 case InlineAsm::isInput:
4959 isInReg = true;
4960 isOutReg = false;
4961 break;
4962 case InlineAsm::isClobber:
4963 isOutReg = true;
4964 isInReg = true;
4965 break;
4966 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004967
4968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004969 MachineFunction &MF = DAG.getMachineFunction();
4970 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004972 // If this is a constraint for a single physreg, or a constraint for a
4973 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004974 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004975 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4976 OpInfo.ConstraintVT);
4977
4978 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004979 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004980 // If this is a FP input in an integer register (or visa versa) insert a bit
4981 // cast of the input value. More generally, handle any case where the input
4982 // value disagrees with the register class we plan to stick this in.
4983 if (OpInfo.Type == InlineAsm::isInput &&
4984 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004985 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004986 // types are identical size, use a bitcast to convert (e.g. two differing
4987 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004988 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004989 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004990 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004991 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004992 OpInfo.ConstraintVT = RegVT;
4993 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4994 // If the input is a FP value and we want it in FP registers, do a
4995 // bitcast to the corresponding integer type. This turns an f64 value
4996 // into i64, which can be passed with two i32 values on a 32-bit
4997 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004998 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00004999 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005000 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005001 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005002 OpInfo.ConstraintVT = RegVT;
5003 }
5004 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005005
Owen Anderson23b9b192009-08-12 00:36:31 +00005006 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005007 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005008
Owen Andersone50ed302009-08-10 22:56:29 +00005009 EVT RegVT;
5010 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005011
5012 // If this is a constraint for a specific physical register, like {r17},
5013 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005014 if (unsigned AssignedReg = PhysReg.first) {
5015 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005016 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005017 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005018
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005019 // Get the actual register value type. This is important, because the user
5020 // may have asked for (e.g.) the AX register in i32 type. We need to
5021 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005022 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005023
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005024 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005025 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005026
5027 // If this is an expanded reference, add the rest of the regs to Regs.
5028 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005029 TargetRegisterClass::iterator I = RC->begin();
5030 for (; *I != AssignedReg; ++I)
5031 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005033 // Already added the first reg.
5034 --NumRegs; ++I;
5035 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005036 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005037 Regs.push_back(*I);
5038 }
5039 }
Bill Wendling651ad132009-12-22 01:25:10 +00005040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5042 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5043 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5044 return;
5045 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005047 // Otherwise, if this was a reference to an LLVM register class, create vregs
5048 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005049 if (const TargetRegisterClass *RC = PhysReg.second) {
5050 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005052 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005053
Evan Chengfb112882009-03-23 08:01:15 +00005054 // Create the appropriate number of virtual registers.
5055 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5056 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005057 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005058
Evan Chengfb112882009-03-23 08:01:15 +00005059 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5060 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005062
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005063 // This is a reference to a register class that doesn't directly correspond
5064 // to an LLVM register class. Allocate NumRegs consecutive, available,
5065 // registers from the class.
5066 std::vector<unsigned> RegClassRegs
5067 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5068 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005069
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005070 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5071 unsigned NumAllocated = 0;
5072 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5073 unsigned Reg = RegClassRegs[i];
5074 // See if this register is available.
5075 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5076 (isInReg && InputRegs.count(Reg))) { // Already used.
5077 // Make sure we find consecutive registers.
5078 NumAllocated = 0;
5079 continue;
5080 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005082 // Check to see if this register is allocatable (i.e. don't give out the
5083 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005084 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5085 if (!RC) { // Couldn't allocate this register.
5086 // Reset NumAllocated to make sure we return consecutive registers.
5087 NumAllocated = 0;
5088 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091 // Okay, this register is good, we can use it.
5092 ++NumAllocated;
5093
5094 // If we allocated enough consecutive registers, succeed.
5095 if (NumAllocated == NumRegs) {
5096 unsigned RegStart = (i-NumAllocated)+1;
5097 unsigned RegEnd = i+1;
5098 // Mark all of the allocated registers used.
5099 for (unsigned i = RegStart; i != RegEnd; ++i)
5100 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005101
5102 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005103 OpInfo.ConstraintVT);
5104 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5105 return;
5106 }
5107 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005109 // Otherwise, we couldn't allocate enough registers for this.
5110}
5111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005112/// visitInlineAsm - Handle a call to an InlineAsm object.
5113///
Dan Gohman46510a72010-04-15 01:51:59 +00005114void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5115 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005116
5117 /// ConstraintOperands - Information about all of the constraints.
5118 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005120 std::set<unsigned> OutputRegs, InputRegs;
5121
5122 // Do a prepass over the constraints, canonicalizing them, and building up the
5123 // ConstraintOperands list.
5124 std::vector<InlineAsm::ConstraintInfo>
5125 ConstraintInfos = IA->ParseConstraints();
5126
Evan Chengda43bcf2008-09-24 00:05:32 +00005127 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005128
Chris Lattner6c147292009-04-30 00:48:50 +00005129 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005130
Chris Lattner6c147292009-04-30 00:48:50 +00005131 // We won't need to flush pending loads if this asm doesn't touch
5132 // memory and is nonvolatile.
5133 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005134 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005135 else
5136 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005138 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5139 unsigned ResNo = 0; // ResNo - The result number of the next output.
5140 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5141 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5142 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005143
Owen Anderson825b72b2009-08-11 20:47:22 +00005144 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005145
5146 // Compute the value type for each operand.
5147 switch (OpInfo.Type) {
5148 case InlineAsm::isOutput:
5149 // Indirect outputs just consume an argument.
5150 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005151 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005152 break;
5153 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005155 // The return value of the call is this value. As such, there is no
5156 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005157 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005158 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5160 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5161 } else {
5162 assert(ResNo == 0 && "Asm only has one result!");
5163 OpVT = TLI.getValueType(CS.getType());
5164 }
5165 ++ResNo;
5166 break;
5167 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005168 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 break;
5170 case InlineAsm::isClobber:
5171 // Nothing to do.
5172 break;
5173 }
5174
5175 // If this is an input or an indirect output, process the call argument.
5176 // BasicBlocks are labels, currently appearing only in asm's.
5177 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005178 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005179 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5180
Dan Gohman46510a72010-04-15 01:51:59 +00005181 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005182 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005183 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005184 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005185 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005186
Owen Anderson1d0be152009-08-13 21:58:54 +00005187 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005188 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005189
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005190 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005191 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005192
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005193 // Second pass over the constraints: compute which constraint option to use
5194 // and assign registers to constraints that want a specific physreg.
5195 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5196 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005197
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005198 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005199 // matching input. If their types mismatch, e.g. one is an integer, the
5200 // other is floating point, or their sizes are different, flag it as an
5201 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005202 if (OpInfo.hasMatchingInput()) {
5203 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005204
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005205 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005206 if ((OpInfo.ConstraintVT.isInteger() !=
5207 Input.ConstraintVT.isInteger()) ||
5208 (OpInfo.ConstraintVT.getSizeInBits() !=
5209 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005210 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005211 " with a matching output constraint of"
5212 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005213 }
5214 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005215 }
5216 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005218 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005219 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005221 // If this is a memory input, and if the operand is not indirect, do what we
5222 // need to to provide an address for the memory input.
5223 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5224 !OpInfo.isIndirect) {
5225 assert(OpInfo.Type == InlineAsm::isInput &&
5226 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005228 // Memory operands really want the address of the value. If we don't have
5229 // an indirect input, put it in the constpool if we can, otherwise spill
5230 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005231
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005232 // If the operand is a float, integer, or vector constant, spill to a
5233 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005234 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005235 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5236 isa<ConstantVector>(OpVal)) {
5237 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5238 TLI.getPointerTy());
5239 } else {
5240 // Otherwise, create a stack slot and emit a store to it before the
5241 // asm.
5242 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005243 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005244 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5245 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005246 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005247 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005248 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005249 OpInfo.CallOperand, StackSlot, NULL, 0,
5250 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251 OpInfo.CallOperand = StackSlot;
5252 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005253
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005254 // There is no longer a Value* corresponding to this operand.
5255 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 // It is now an indirect operand.
5258 OpInfo.isIndirect = true;
5259 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 // If this constraint is for a specific register, allocate it before
5262 // anything else.
5263 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005264 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005265 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005266
Bill Wendling651ad132009-12-22 01:25:10 +00005267 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005270 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005271 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5272 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005273
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005274 // C_Register operands have already been allocated, Other/Memory don't need
5275 // to be.
5276 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005277 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005278 }
5279
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005280 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5281 std::vector<SDValue> AsmNodeOperands;
5282 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5283 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005284 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5285 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005286
Chris Lattnerdecc2672010-04-07 05:20:54 +00005287 // If we have a !srcloc metadata node associated with it, we want to attach
5288 // this to the ultimately generated inline asm machineinstr. To do this, we
5289 // pass in the third operand as this (potentially null) inline asm MDNode.
5290 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5291 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 // Loop over all of the inputs, copying the operand values into the
5294 // appropriate registers and processing the output regs.
5295 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005297 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5298 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005299
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005300 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5301 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5302
5303 switch (OpInfo.Type) {
5304 case InlineAsm::isOutput: {
5305 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5306 OpInfo.ConstraintType != TargetLowering::C_Register) {
5307 // Memory output, or 'other' output (e.g. 'X' constraint).
5308 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5309
5310 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005311 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5312 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 TLI.getPointerTy()));
5314 AsmNodeOperands.push_back(OpInfo.CallOperand);
5315 break;
5316 }
5317
5318 // Otherwise, this is a register or register class output.
5319
5320 // Copy the output from the appropriate register. Find a register that
5321 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005322 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005323 report_fatal_error("Couldn't allocate output reg for constraint '" +
5324 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325
5326 // If this is an indirect operand, store through the pointer after the
5327 // asm.
5328 if (OpInfo.isIndirect) {
5329 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5330 OpInfo.CallOperandVal));
5331 } else {
5332 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005333 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005334 // Concatenate this output onto the outputs list.
5335 RetValRegs.append(OpInfo.AssignedRegs);
5336 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005337
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005338 // Add information to the INLINEASM node to know that this register is
5339 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005340 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005341 InlineAsm::Kind_RegDefEarlyClobber :
5342 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005343 false,
5344 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005345 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005346 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005347 break;
5348 }
5349 case InlineAsm::isInput: {
5350 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005351
Chris Lattner6bdcda32008-10-17 16:47:46 +00005352 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005353 // If this is required to match an output register we have already set,
5354 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005355 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005356
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005357 // Scan until we find the definition we already emitted of this operand.
5358 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005359 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005360 for (; OperandNo; --OperandNo) {
5361 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005362 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005363 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005364 assert((InlineAsm::isRegDefKind(OpFlag) ||
5365 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5366 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005367 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005368 }
5369
Evan Cheng697cbbf2009-03-20 18:03:34 +00005370 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005371 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005372 if (InlineAsm::isRegDefKind(OpFlag) ||
5373 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005374 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005375 if (OpInfo.isIndirect) {
5376 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005377 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005378 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5379 " don't know how to handle tied "
5380 "indirect register inputs");
5381 }
5382
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005383 RegsForValue MatchedRegs;
5384 MatchedRegs.TLI = &TLI;
5385 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005386 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005387 MatchedRegs.RegVTs.push_back(RegVT);
5388 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005389 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005390 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005391 MatchedRegs.Regs.push_back
5392 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005393
5394 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005395 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005396 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005397 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005398 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005399 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005401 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005402
5403 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5404 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5405 "Unexpected number of operands");
5406 // Add information to the INLINEASM node to know about this input.
5407 // See InlineAsm.h isUseOperandTiedToDef.
5408 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5409 OpInfo.getMatchedOperand());
5410 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5411 TLI.getPointerTy()));
5412 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5413 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005414 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005415
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005416 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005417 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005419
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 std::vector<SDValue> Ops;
5421 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005422 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005423 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005424 report_fatal_error("Invalid operand for inline asm constraint '" +
5425 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005426
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005427 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005428 unsigned ResOpType =
5429 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005430 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005431 TLI.getPointerTy()));
5432 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5433 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005434 }
5435
5436 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005437 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5438 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5439 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005441 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005442 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005443 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005444 TLI.getPointerTy()));
5445 AsmNodeOperands.push_back(InOperandVal);
5446 break;
5447 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005449 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5450 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5451 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005452 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453 "Don't know how to handle indirect register inputs yet!");
5454
5455 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005456 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005457 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005458 report_fatal_error("Couldn't allocate input reg for constraint '" +
5459 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005460
Dale Johannesen66978ee2009-01-31 02:22:37 +00005461 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005462 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005463
Chris Lattnerdecc2672010-04-07 05:20:54 +00005464 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005465 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005466 break;
5467 }
5468 case InlineAsm::isClobber: {
5469 // Add the clobbered value to the operand list, so that the register
5470 // allocator is aware that the physreg got clobbered.
5471 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005472 OpInfo.AssignedRegs.AddInlineAsmOperands(
5473 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005474 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005475 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005476 break;
5477 }
5478 }
5479 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005480
Chris Lattnerdecc2672010-04-07 05:20:54 +00005481 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005482 AsmNodeOperands[0] = Chain;
5483 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005484
Dale Johannesen66978ee2009-01-31 02:22:37 +00005485 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005486 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005487 &AsmNodeOperands[0], AsmNodeOperands.size());
5488 Flag = Chain.getValue(1);
5489
5490 // If this asm returns a register value, copy the result from that register
5491 // and set it as the value of the call.
5492 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005493 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005494 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005495
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005496 // FIXME: Why don't we do this for inline asms with MRVs?
5497 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005498 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005499
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005500 // If any of the results of the inline asm is a vector, it may have the
5501 // wrong width/num elts. This can happen for register classes that can
5502 // contain multiple different value types. The preg or vreg allocated may
5503 // not have the same VT as was expected. Convert it to the right type
5504 // with bit_convert.
5505 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005506 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005507 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005508
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005509 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005510 ResultType.isInteger() && Val.getValueType().isInteger()) {
5511 // If a result value was tied to an input value, the computed result may
5512 // have a wider width than the expected result. Extract the relevant
5513 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005514 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005515 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005516
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005517 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005518 }
Dan Gohman95915732008-10-18 01:03:45 +00005519
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005520 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005521 // Don't need to use this as a chain in this case.
5522 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5523 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005524 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005525
Dan Gohman46510a72010-04-15 01:51:59 +00005526 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005527
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005528 // Process indirect outputs, first output all of the flagged copies out of
5529 // physregs.
5530 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5531 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005532 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005533 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005534 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005535 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5536 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005537
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005538 // Emit the non-flagged stores from the physregs.
5539 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005540 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5541 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5542 StoresToEmit[i].first,
5543 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005544 StoresToEmit[i].second, 0,
5545 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005546 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005547 }
5548
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005550 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005552
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553 DAG.setRoot(Chain);
5554}
5555
Dan Gohman46510a72010-04-15 01:51:59 +00005556void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005557 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5558 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005559 getValue(I.getOperand(1)),
5560 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005561}
5562
Dan Gohman46510a72010-04-15 01:51:59 +00005563void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005564 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5565 getRoot(), getValue(I.getOperand(0)),
5566 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005567 setValue(&I, V);
5568 DAG.setRoot(V.getValue(1));
5569}
5570
Dan Gohman46510a72010-04-15 01:51:59 +00005571void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005572 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5573 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005574 getValue(I.getOperand(1)),
5575 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005576}
5577
Dan Gohman46510a72010-04-15 01:51:59 +00005578void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005579 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5580 MVT::Other, getRoot(),
5581 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005582 getValue(I.getOperand(2)),
5583 DAG.getSrcValue(I.getOperand(1)),
5584 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005585}
5586
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005587/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005588/// implementation, which just calls LowerCall.
5589/// FIXME: When all targets are
5590/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005591std::pair<SDValue, SDValue>
5592TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5593 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005594 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005595 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005596 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005597 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005598 ArgListTy &Args, SelectionDAG &DAG,
5599 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005600 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005601 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005603 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005604 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5605 for (unsigned Value = 0, NumValues = ValueVTs.size();
5606 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005607 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005608 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005609 SDValue Op = SDValue(Args[i].Node.getNode(),
5610 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005611 ISD::ArgFlagsTy Flags;
5612 unsigned OriginalAlignment =
5613 getTargetData()->getABITypeAlignment(ArgTy);
5614
5615 if (Args[i].isZExt)
5616 Flags.setZExt();
5617 if (Args[i].isSExt)
5618 Flags.setSExt();
5619 if (Args[i].isInReg)
5620 Flags.setInReg();
5621 if (Args[i].isSRet)
5622 Flags.setSRet();
5623 if (Args[i].isByVal) {
5624 Flags.setByVal();
5625 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5626 const Type *ElementTy = Ty->getElementType();
5627 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005628 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005629 // For ByVal, alignment should come from FE. BE will guess if this
5630 // info is not there but there are cases it cannot get right.
5631 if (Args[i].Alignment)
5632 FrameAlign = Args[i].Alignment;
5633 Flags.setByValAlign(FrameAlign);
5634 Flags.setByValSize(FrameSize);
5635 }
5636 if (Args[i].isNest)
5637 Flags.setNest();
5638 Flags.setOrigAlign(OriginalAlignment);
5639
Owen Anderson23b9b192009-08-12 00:36:31 +00005640 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5641 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005642 SmallVector<SDValue, 4> Parts(NumParts);
5643 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5644
5645 if (Args[i].isSExt)
5646 ExtendKind = ISD::SIGN_EXTEND;
5647 else if (Args[i].isZExt)
5648 ExtendKind = ISD::ZERO_EXTEND;
5649
Bill Wendling46ada192010-03-02 01:55:18 +00005650 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005651 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652
Dan Gohman98ca4f22009-08-05 01:29:28 +00005653 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005655 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5656 if (NumParts > 1 && j == 0)
5657 MyFlags.Flags.setSplit();
5658 else if (j != 0)
5659 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005660
Dan Gohman98ca4f22009-08-05 01:29:28 +00005661 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005662 }
5663 }
5664 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005665
Dan Gohman98ca4f22009-08-05 01:29:28 +00005666 // Handle the incoming return values from the call.
5667 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005668 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005669 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005670 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005671 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005672 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5673 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005674 for (unsigned i = 0; i != NumRegs; ++i) {
5675 ISD::InputArg MyFlags;
5676 MyFlags.VT = RegisterVT;
5677 MyFlags.Used = isReturnValueUsed;
5678 if (RetSExt)
5679 MyFlags.Flags.setSExt();
5680 if (RetZExt)
5681 MyFlags.Flags.setZExt();
5682 if (isInreg)
5683 MyFlags.Flags.setInReg();
5684 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005685 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005686 }
5687
Dan Gohman98ca4f22009-08-05 01:29:28 +00005688 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005689 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005690 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005691
5692 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005693 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005694 "LowerCall didn't return a valid chain!");
5695 assert((!isTailCall || InVals.empty()) &&
5696 "LowerCall emitted a return value for a tail call!");
5697 assert((isTailCall || InVals.size() == Ins.size()) &&
5698 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005699
5700 // For a tail call, the return value is merely live-out and there aren't
5701 // any nodes in the DAG representing it. Return a special value to
5702 // indicate that a tail call has been emitted and no more Instructions
5703 // should be processed in the current block.
5704 if (isTailCall) {
5705 DAG.setRoot(Chain);
5706 return std::make_pair(SDValue(), SDValue());
5707 }
5708
Evan Chengaf1871f2010-03-11 19:38:18 +00005709 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5710 assert(InVals[i].getNode() &&
5711 "LowerCall emitted a null value!");
5712 assert(Ins[i].VT == InVals[i].getValueType() &&
5713 "LowerCall emitted a value with the wrong type!");
5714 });
5715
Dan Gohman98ca4f22009-08-05 01:29:28 +00005716 // Collect the legal value parts into potentially illegal values
5717 // that correspond to the original function's return values.
5718 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5719 if (RetSExt)
5720 AssertOp = ISD::AssertSext;
5721 else if (RetZExt)
5722 AssertOp = ISD::AssertZext;
5723 SmallVector<SDValue, 4> ReturnValues;
5724 unsigned CurReg = 0;
5725 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005726 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005727 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5728 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005729
Bill Wendling46ada192010-03-02 01:55:18 +00005730 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005731 NumRegs, RegisterVT, VT,
5732 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005733 CurReg += NumRegs;
5734 }
5735
5736 // For a function returning void, there is no return value. We can't create
5737 // such a node, so we just return a null return value in that case. In
5738 // that case, nothing will actualy look at the value.
5739 if (ReturnValues.empty())
5740 return std::make_pair(SDValue(), Chain);
5741
5742 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5743 DAG.getVTList(&RetTys[0], RetTys.size()),
5744 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005745 return std::make_pair(Res, Chain);
5746}
5747
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005748void TargetLowering::LowerOperationWrapper(SDNode *N,
5749 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005750 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005751 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005752 if (Res.getNode())
5753 Results.push_back(Res);
5754}
5755
Dan Gohmand858e902010-04-17 15:26:15 +00005756SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005757 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758 return SDValue();
5759}
5760
Dan Gohman46510a72010-04-15 01:51:59 +00005761void
5762SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005763 SDValue Op = getValue(V);
5764 assert((Op.getOpcode() != ISD::CopyFromReg ||
5765 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5766 "Copy from a reg to the same reg!");
5767 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5768
Owen Anderson23b9b192009-08-12 00:36:31 +00005769 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005771 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005772 PendingExports.push_back(Chain);
5773}
5774
5775#include "llvm/CodeGen/SelectionDAGISel.h"
5776
Dan Gohman46510a72010-04-15 01:51:59 +00005777void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005778 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005779 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005780 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005781 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005782 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005783 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005784 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005786 // Check whether the function can return without sret-demotion.
5787 SmallVector<EVT, 4> OutVTs;
5788 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005789 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005790 OutVTs, OutsFlags, TLI);
5791 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5792
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005793 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005794 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005795 if (!FLI.CanLowerReturn) {
5796 // Put in an sret pointer parameter before all the other parameters.
5797 SmallVector<EVT, 1> ValueVTs;
5798 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5799
5800 // NOTE: Assuming that a pointer will never break down to more than one VT
5801 // or one register.
5802 ISD::ArgFlagsTy Flags;
5803 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005804 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005805 ISD::InputArg RetArg(Flags, RegisterVT, true);
5806 Ins.push_back(RetArg);
5807 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005808
Dan Gohman98ca4f22009-08-05 01:29:28 +00005809 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005810 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005811 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005812 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005813 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005814 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5815 bool isArgValueUsed = !I->use_empty();
5816 for (unsigned Value = 0, NumValues = ValueVTs.size();
5817 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005818 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005819 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005820 ISD::ArgFlagsTy Flags;
5821 unsigned OriginalAlignment =
5822 TD->getABITypeAlignment(ArgTy);
5823
5824 if (F.paramHasAttr(Idx, Attribute::ZExt))
5825 Flags.setZExt();
5826 if (F.paramHasAttr(Idx, Attribute::SExt))
5827 Flags.setSExt();
5828 if (F.paramHasAttr(Idx, Attribute::InReg))
5829 Flags.setInReg();
5830 if (F.paramHasAttr(Idx, Attribute::StructRet))
5831 Flags.setSRet();
5832 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5833 Flags.setByVal();
5834 const PointerType *Ty = cast<PointerType>(I->getType());
5835 const Type *ElementTy = Ty->getElementType();
5836 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5837 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5838 // For ByVal, alignment should be passed from FE. BE will guess if
5839 // this info is not there but there are cases it cannot get right.
5840 if (F.getParamAlignment(Idx))
5841 FrameAlign = F.getParamAlignment(Idx);
5842 Flags.setByValAlign(FrameAlign);
5843 Flags.setByValSize(FrameSize);
5844 }
5845 if (F.paramHasAttr(Idx, Attribute::Nest))
5846 Flags.setNest();
5847 Flags.setOrigAlign(OriginalAlignment);
5848
Owen Anderson23b9b192009-08-12 00:36:31 +00005849 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5850 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005851 for (unsigned i = 0; i != NumRegs; ++i) {
5852 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5853 if (NumRegs > 1 && i == 0)
5854 MyFlags.Flags.setSplit();
5855 // if it isn't first piece, alignment must be 1
5856 else if (i > 0)
5857 MyFlags.Flags.setOrigAlign(1);
5858 Ins.push_back(MyFlags);
5859 }
5860 }
5861 }
5862
5863 // Call the target to set up the argument values.
5864 SmallVector<SDValue, 8> InVals;
5865 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5866 F.isVarArg(), Ins,
5867 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005868
5869 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005871 "LowerFormalArguments didn't return a valid chain!");
5872 assert(InVals.size() == Ins.size() &&
5873 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005874 DEBUG({
5875 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5876 assert(InVals[i].getNode() &&
5877 "LowerFormalArguments emitted a null value!");
5878 assert(Ins[i].VT == InVals[i].getValueType() &&
5879 "LowerFormalArguments emitted a value with the wrong type!");
5880 }
5881 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005882
Dan Gohman5e866062009-08-06 15:37:27 +00005883 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005884 DAG.setRoot(NewRoot);
5885
5886 // Set up the argument values.
5887 unsigned i = 0;
5888 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005889 if (!FLI.CanLowerReturn) {
5890 // Create a virtual register for the sret pointer, and put in a copy
5891 // from the sret argument into it.
5892 SmallVector<EVT, 1> ValueVTs;
5893 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5894 EVT VT = ValueVTs[0];
5895 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5896 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005897 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005898 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005899
Dan Gohman2048b852009-11-23 18:04:58 +00005900 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005901 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5902 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5903 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005904 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5905 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005906 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005907
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005908 // i indexes lowered arguments. Bump it past the hidden sret argument.
5909 // Idx indexes LLVM arguments. Don't touch it.
5910 ++i;
5911 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005912
Dan Gohman46510a72010-04-15 01:51:59 +00005913 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005914 ++I, ++Idx) {
5915 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005916 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005917 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005918 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005919 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005920 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005921 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5922 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005923
5924 if (!I->use_empty()) {
5925 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5926 if (F.paramHasAttr(Idx, Attribute::SExt))
5927 AssertOp = ISD::AssertSext;
5928 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5929 AssertOp = ISD::AssertZext;
5930
Bill Wendling46ada192010-03-02 01:55:18 +00005931 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00005932 NumParts, PartVT, VT,
5933 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005934 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005935
Dan Gohman98ca4f22009-08-05 01:29:28 +00005936 i += NumParts;
5937 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005938
Dan Gohman98ca4f22009-08-05 01:29:28 +00005939 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00005940 SDValue Res;
5941 if (!ArgValues.empty())
5942 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
5943 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00005944 SDB->setValue(I, Res);
5945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005946 // If this argument is live outside of the entry block, insert a copy from
5947 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005948 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005949 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005950 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005951
Dan Gohman98ca4f22009-08-05 01:29:28 +00005952 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005953
5954 // Finally, if the target has anything special to do, allow it to do so.
5955 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00005956 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957}
5958
5959/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5960/// ensure constants are generated when needed. Remember the virtual registers
5961/// that need to be added to the Machine PHI nodes as input. We cannot just
5962/// directly add them, because expansion might result in multiple MBB's for one
5963/// BB. As such, the start of the BB might correspond to a different MBB than
5964/// the end.
5965///
5966void
Dan Gohmanf81eca02010-04-22 20:46:50 +00005967SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00005968 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969
5970 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5971
5972 // Check successor nodes' PHI nodes that expect a constant to be available
5973 // from this block.
5974 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00005975 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00005977 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979 // If this terminator has multiple identical successors (common for
5980 // switches), only handle each succ once.
5981 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005984
5985 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5986 // nodes and Machine PHI nodes, but the incoming operands have not been
5987 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00005988 for (BasicBlock::const_iterator I = SuccBB->begin();
5989 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005990 // Ignore dead phi's.
5991 if (PN->use_empty()) continue;
5992
5993 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00005994 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005995
Dan Gohman46510a72010-04-15 01:51:59 +00005996 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00005997 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005998 if (RegOut == 0) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00005999 RegOut = FuncInfo.CreateRegForValue(C);
6000 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 }
6002 Reg = RegOut;
6003 } else {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006004 Reg = FuncInfo.ValueMap[PHIOp];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006005 if (Reg == 0) {
6006 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006007 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006008 "Didn't codegen value into a register!??");
Dan Gohmanf81eca02010-04-22 20:46:50 +00006009 Reg = FuncInfo.CreateRegForValue(PHIOp);
6010 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006011 }
6012 }
6013
6014 // Remember that this register needs to added to the machine PHI node as
6015 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006016 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006017 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6018 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006019 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006020 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006021 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006022 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006023 Reg += NumRegisters;
6024 }
6025 }
6026 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006027 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006028}