blob: f883209b568a19a4f57ca6750350a3ea3928636d [file] [log] [blame]
Chris Lattner1c08c712005-01-07 07:47:53 +00001//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner1c08c712005-01-07 07:47:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner1c08c712005-01-07 07:47:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAGISel class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Anton Korobeynikov5502bf62007-04-04 21:14:49 +000015#include "llvm/ADT/BitVector.h"
Jim Laskeyc7c3f112006-10-16 20:52:31 +000016#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000017#include "llvm/CodeGen/SelectionDAGISel.h"
Evan Chenga9c20912006-01-21 02:32:06 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Anton Korobeynikov5502bf62007-04-04 21:14:49 +000019#include "llvm/Constants.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000020#include "llvm/CallingConv.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Chris Lattner36ce6912005-11-29 06:21:05 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerce7518c2006-01-26 22:24:51 +000024#include "llvm/InlineAsm.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000025#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
Jim Laskey43970fe2006-03-23 18:06:46 +000027#include "llvm/IntrinsicInst.h"
Reid Spencer5694b6e2007-04-09 06:17:21 +000028#include "llvm/ParameterAttributes.h"
Gordon Henriksence224772008-01-07 01:30:38 +000029#include "llvm/CodeGen/Collector.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
34#include "llvm/CodeGen/MachineModuleInfo.h"
35#include "llvm/CodeGen/MachineRegisterInfo.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000036#include "llvm/CodeGen/SchedulerRegistry.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000037#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000038#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000039#include "llvm/Target/TargetData.h"
40#include "llvm/Target/TargetFrameInfo.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetLowering.h"
43#include "llvm/Target/TargetMachine.h"
Vladimir Prus12472912006-05-23 13:43:15 +000044#include "llvm/Target/TargetOptions.h"
Chris Lattner7c0104b2005-11-09 04:45:33 +000045#include "llvm/Support/MathExtras.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000046#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000047#include "llvm/Support/Compiler.h"
Jeff Cohen7e881032006-02-24 02:52:40 +000048#include <algorithm>
Chris Lattner1c08c712005-01-07 07:47:53 +000049using namespace llvm;
50
Chris Lattnerda8abb02005-09-01 18:44:10 +000051#ifndef NDEBUG
Chris Lattner7944d9d2005-01-12 03:41:21 +000052static cl::opt<bool>
Evan Chenga9c20912006-01-21 02:32:06 +000053ViewISelDAGs("view-isel-dags", cl::Hidden,
54 cl::desc("Pop up a window to show isel dags as they are selected"));
55static cl::opt<bool>
56ViewSchedDAGs("view-sched-dags", cl::Hidden,
57 cl::desc("Pop up a window to show sched dags as they are processed"));
Dan Gohman3e1a7ae2007-08-28 20:32:58 +000058static cl::opt<bool>
59ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
Chris Lattner5bab7852008-01-25 17:24:52 +000060 cl::desc("Pop up a window to show SUnit dags after they are processed"));
Chris Lattner7944d9d2005-01-12 03:41:21 +000061#else
Dan Gohman3e1a7ae2007-08-28 20:32:58 +000062static const bool ViewISelDAGs = 0, ViewSchedDAGs = 0, ViewSUnitDAGs = 0;
Chris Lattner7944d9d2005-01-12 03:41:21 +000063#endif
64
Jim Laskeyeb577ba2006-08-02 12:30:23 +000065//===---------------------------------------------------------------------===//
66///
67/// RegisterScheduler class - Track the registration of instruction schedulers.
68///
69//===---------------------------------------------------------------------===//
70MachinePassRegistry RegisterScheduler::Registry;
71
72//===---------------------------------------------------------------------===//
73///
74/// ISHeuristic command line option for instruction schedulers.
75///
76//===---------------------------------------------------------------------===//
Evan Cheng4ef10862006-01-23 07:01:07 +000077namespace {
Jim Laskeyeb577ba2006-08-02 12:30:23 +000078 cl::opt<RegisterScheduler::FunctionPassCtor, false,
79 RegisterPassParser<RegisterScheduler> >
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000080 ISHeuristic("pre-RA-sched",
Chris Lattner3700f902006-08-03 00:18:59 +000081 cl::init(&createDefaultScheduler),
Chris Lattner5bab7852008-01-25 17:24:52 +000082 cl::desc("Instruction schedulers available (before register"
83 " allocation):"));
Jim Laskey13ec7022006-08-01 14:21:23 +000084
Jim Laskey9ff542f2006-08-01 18:29:48 +000085 static RegisterScheduler
Jim Laskey9373beb2006-08-01 19:14:14 +000086 defaultListDAGScheduler("default", " Best scheduler for the target",
87 createDefaultScheduler);
Evan Cheng4ef10862006-01-23 07:01:07 +000088} // namespace
89
Evan Cheng5c807602008-02-26 02:33:44 +000090namespace { struct SDISelAsmOperandInfo; }
Chris Lattnerbf996f12007-04-30 17:29:31 +000091
Chris Lattnerf899fce2008-04-27 23:48:12 +000092/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
93/// MVT::ValueTypes that represent all the individual underlying
94/// non-aggregate types that comprise it.
95static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
96 SmallVectorImpl<MVT::ValueType> &ValueVTs) {
97 // Given a struct type, recursively traverse the elements.
98 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
99 for (StructType::element_iterator EI = STy->element_begin(),
100 EB = STy->element_end();
101 EI != EB; ++EI)
102 ComputeValueVTs(TLI, *EI, ValueVTs);
103 return;
Dan Gohman23ce5022008-04-25 18:27:55 +0000104 }
Chris Lattnerf899fce2008-04-27 23:48:12 +0000105 // Given an array type, recursively traverse the elements.
106 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
107 const Type *EltTy = ATy->getElementType();
108 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
109 ComputeValueVTs(TLI, EltTy, ValueVTs);
110 return;
111 }
112 // Base case: we can get an MVT::ValueType for this LLVM IR type.
113 ValueVTs.push_back(TLI.getValueType(Ty));
114}
Dan Gohman23ce5022008-04-25 18:27:55 +0000115
Chris Lattnerf899fce2008-04-27 23:48:12 +0000116namespace {
Chris Lattner864635a2006-02-22 22:37:12 +0000117 /// RegsForValue - This struct represents the physical registers that a
118 /// particular value is assigned and the type information about the value.
119 /// This is needed because values can be promoted into larger registers and
120 /// expanded into multiple smaller registers than the value.
Chris Lattner95255282006-06-28 23:17:24 +0000121 struct VISIBILITY_HIDDEN RegsForValue {
Dan Gohman23ce5022008-04-25 18:27:55 +0000122 /// TLI - The TargetLowering object.
123 const TargetLowering *TLI;
124
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000125 /// Regs - This list holds the register (for legal and promoted values)
Chris Lattner864635a2006-02-22 22:37:12 +0000126 /// or register set (for expanded values) that the value should be assigned
127 /// to.
Chris Lattner8eaff042008-04-28 06:02:19 +0000128 SmallVector<unsigned, 4> Regs;
Chris Lattner864635a2006-02-22 22:37:12 +0000129
Dan Gohman23ce5022008-04-25 18:27:55 +0000130 /// RegVTs - The value types of the registers. This is the same size
131 /// as ValueVTs; every register contributing to a given value must
132 /// have the same type. When Regs contains all virtual registers, the
133 /// contents of RegVTs is redundant with TLI's getRegisterType member
134 /// function, however when Regs contains physical registers, it is
135 /// necessary to have a separate record of the types.
Chris Lattner864635a2006-02-22 22:37:12 +0000136 ///
Dan Gohman23ce5022008-04-25 18:27:55 +0000137 SmallVector<MVT::ValueType, 4> RegVTs;
Chris Lattner864635a2006-02-22 22:37:12 +0000138
Dan Gohman23ce5022008-04-25 18:27:55 +0000139 /// ValueVTs - The value types of the values, which may be promoted
140 /// or synthesized from one or more registers.
141 SmallVector<MVT::ValueType, 4> ValueVTs;
Chris Lattner864635a2006-02-22 22:37:12 +0000142
Dan Gohman23ce5022008-04-25 18:27:55 +0000143 RegsForValue() : TLI(0) {}
Chris Lattner864635a2006-02-22 22:37:12 +0000144
Dan Gohman23ce5022008-04-25 18:27:55 +0000145 RegsForValue(const TargetLowering &tli,
146 unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt)
147 : TLI(&tli), Regs(1, Reg), RegVTs(1, regvt), ValueVTs(1, valuevt) {}
148 RegsForValue(const TargetLowering &tli,
Chris Lattnerb606dba2008-04-28 06:44:42 +0000149 const SmallVector<unsigned, 4> &regs,
Chris Lattner864635a2006-02-22 22:37:12 +0000150 MVT::ValueType regvt, MVT::ValueType valuevt)
Dan Gohman23ce5022008-04-25 18:27:55 +0000151 : TLI(&tli), Regs(regs), RegVTs(1, regvt), ValueVTs(1, valuevt) {}
152 RegsForValue(const TargetLowering &tli,
Chris Lattnerb606dba2008-04-28 06:44:42 +0000153 const SmallVector<unsigned, 4> &regs,
Dan Gohman23ce5022008-04-25 18:27:55 +0000154 const SmallVector<MVT::ValueType, 4> &regvts,
155 const SmallVector<MVT::ValueType, 4> &valuevts)
156 : TLI(&tli), Regs(regs), RegVTs(regvts), ValueVTs(valuevts) {}
157 RegsForValue(const TargetLowering &tli,
158 unsigned Reg, const Type *Ty) : TLI(&tli) {
159 ComputeValueVTs(tli, Ty, ValueVTs);
160
Dan Gohmanb20d4f82008-04-28 17:42:03 +0000161 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Dan Gohman23ce5022008-04-25 18:27:55 +0000162 MVT::ValueType ValueVT = ValueVTs[Value];
163 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
164 MVT::ValueType RegisterVT = TLI->getRegisterType(ValueVT);
165 for (unsigned i = 0; i != NumRegs; ++i)
166 Regs.push_back(Reg + i);
167 RegVTs.push_back(RegisterVT);
168 Reg += NumRegs;
169 }
Chris Lattner864635a2006-02-22 22:37:12 +0000170 }
171
172 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Dan Gohman23ce5022008-04-25 18:27:55 +0000173 /// this value and returns the result as a ValueVTs value. This uses
Chris Lattner864635a2006-02-22 22:37:12 +0000174 /// Chain/Flag as the input and updates them for the output Chain/Flag.
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000175 /// If the Flag pointer is NULL, no flag is used.
Chris Lattner864635a2006-02-22 22:37:12 +0000176 SDOperand getCopyFromRegs(SelectionDAG &DAG,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000177 SDOperand &Chain, SDOperand *Flag) const;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000178
179 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
180 /// specified value into the registers specified by this object. This uses
181 /// Chain/Flag as the input and updates them for the output Chain/Flag.
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000182 /// If the Flag pointer is NULL, no flag is used.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000183 void getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000184 SDOperand &Chain, SDOperand *Flag) const;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000185
186 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
187 /// operand list. This adds the code marker and includes the number of
188 /// values added into it.
189 void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
Chris Lattner9f6637d2006-02-23 20:06:57 +0000190 std::vector<SDOperand> &Ops) const;
Chris Lattner864635a2006-02-22 22:37:12 +0000191 };
192}
Evan Cheng4ef10862006-01-23 07:01:07 +0000193
Chris Lattner1c08c712005-01-07 07:47:53 +0000194namespace llvm {
195 //===--------------------------------------------------------------------===//
Jim Laskey9373beb2006-08-01 19:14:14 +0000196 /// createDefaultScheduler - This creates an instruction scheduler appropriate
197 /// for the target.
198 ScheduleDAG* createDefaultScheduler(SelectionDAGISel *IS,
199 SelectionDAG *DAG,
200 MachineBasicBlock *BB) {
201 TargetLowering &TLI = IS->getTargetLowering();
202
203 if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) {
204 return createTDListDAGScheduler(IS, DAG, BB);
205 } else {
206 assert(TLI.getSchedulingPreference() ==
207 TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
208 return createBURRListDAGScheduler(IS, DAG, BB);
209 }
210 }
211
212
213 //===--------------------------------------------------------------------===//
Chris Lattner1c08c712005-01-07 07:47:53 +0000214 /// FunctionLoweringInfo - This contains information that is global to a
215 /// function that is used when lowering a region of the function.
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000216 class FunctionLoweringInfo {
217 public:
Chris Lattner1c08c712005-01-07 07:47:53 +0000218 TargetLowering &TLI;
219 Function &Fn;
220 MachineFunction &MF;
Chris Lattner84bc5422007-12-31 04:13:23 +0000221 MachineRegisterInfo &RegInfo;
Chris Lattner1c08c712005-01-07 07:47:53 +0000222
223 FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
224
225 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
226 std::map<const BasicBlock*, MachineBasicBlock *> MBBMap;
227
228 /// ValueMap - Since we emit code for the function a basic block at a time,
229 /// we must remember which virtual registers hold the values for
230 /// cross-basic-block values.
Chris Lattner9f24ad72007-02-04 01:35:11 +0000231 DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner1c08c712005-01-07 07:47:53 +0000232
233 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
234 /// the entry block. This allows the allocas to be efficiently referenced
235 /// anywhere in the function.
236 std::map<const AllocaInst*, int> StaticAllocaMap;
237
Duncan Sandsf4070822007-06-15 19:04:19 +0000238#ifndef NDEBUG
239 SmallSet<Instruction*, 8> CatchInfoLost;
240 SmallSet<Instruction*, 8> CatchInfoFound;
241#endif
242
Chris Lattner1c08c712005-01-07 07:47:53 +0000243 unsigned MakeReg(MVT::ValueType VT) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000244 return RegInfo.createVirtualRegister(TLI.getRegClassFor(VT));
Chris Lattner1c08c712005-01-07 07:47:53 +0000245 }
Chris Lattner571e4342006-10-27 21:36:01 +0000246
247 /// isExportedInst - Return true if the specified value is an instruction
248 /// exported from its block.
249 bool isExportedInst(const Value *V) {
250 return ValueMap.count(V);
251 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252
Chris Lattner3c384492006-03-16 19:51:18 +0000253 unsigned CreateRegForValue(const Value *V);
254
Chris Lattner1c08c712005-01-07 07:47:53 +0000255 unsigned InitializeRegForValue(const Value *V) {
256 unsigned &R = ValueMap[V];
257 assert(R == 0 && "Already initialized this value register!");
258 return R = CreateRegForValue(V);
259 }
260 };
261}
262
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000263/// isSelector - Return true if this instruction is a call to the
264/// eh.selector intrinsic.
265static bool isSelector(Instruction *I) {
Duncan Sandsf4070822007-06-15 19:04:19 +0000266 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000267 return (II->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
268 II->getIntrinsicID() == Intrinsic::eh_selector_i64);
Duncan Sandsf4070822007-06-15 19:04:19 +0000269 return false;
270}
271
Chris Lattner1c08c712005-01-07 07:47:53 +0000272/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
Nate Begemanf15485a2006-03-27 01:32:24 +0000273/// PHI nodes or outside of the basic block that defines it, or used by a
Andrew Lenharthab0b9492008-02-21 06:45:13 +0000274/// switch or atomic instruction, which may expand to multiple basic blocks.
Chris Lattner1c08c712005-01-07 07:47:53 +0000275static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
276 if (isa<PHINode>(I)) return true;
277 BasicBlock *BB = I->getParent();
278 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
Nate Begemanf15485a2006-03-27 01:32:24 +0000279 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) ||
Chris Lattner571e4342006-10-27 21:36:01 +0000280 // FIXME: Remove switchinst special case.
Nate Begemanf15485a2006-03-27 01:32:24 +0000281 isa<SwitchInst>(*UI))
Chris Lattner1c08c712005-01-07 07:47:53 +0000282 return true;
283 return false;
284}
285
Chris Lattnerbf209482005-10-30 19:42:35 +0000286/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
Nate Begemanf15485a2006-03-27 01:32:24 +0000287/// entry block, return true. This includes arguments used by switches, since
288/// the switch may expand into multiple basic blocks.
Chris Lattnerbf209482005-10-30 19:42:35 +0000289static bool isOnlyUsedInEntryBlock(Argument *A) {
290 BasicBlock *Entry = A->getParent()->begin();
291 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
Nate Begemanf15485a2006-03-27 01:32:24 +0000292 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
Chris Lattnerbf209482005-10-30 19:42:35 +0000293 return false; // Use not in entry block.
294 return true;
295}
296
Chris Lattner1c08c712005-01-07 07:47:53 +0000297FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000298 Function &fn, MachineFunction &mf)
Chris Lattner84bc5422007-12-31 04:13:23 +0000299 : TLI(tli), Fn(fn), MF(mf), RegInfo(MF.getRegInfo()) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000300
Chris Lattnerbf209482005-10-30 19:42:35 +0000301 // Create a vreg for each argument register that is not dead and is used
302 // outside of the entry block for the function.
303 for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end();
304 AI != E; ++AI)
305 if (!isOnlyUsedInEntryBlock(AI))
306 InitializeRegForValue(AI);
307
Chris Lattner1c08c712005-01-07 07:47:53 +0000308 // Initialize the mapping of values to registers. This is only set up for
309 // instruction values that are used outside of the block that defines
310 // them.
Jeff Cohen2aeaf4e2005-10-01 03:57:14 +0000311 Function::iterator BB = Fn.begin(), EB = Fn.end();
Chris Lattner1c08c712005-01-07 07:47:53 +0000312 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
313 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
Reid Spencerb83eb642006-10-20 07:07:24 +0000314 if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000315 const Type *Ty = AI->getAllocatedType();
Duncan Sands514ab342007-11-01 20:53:16 +0000316 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Nate Begemanae232e72005-11-06 09:00:38 +0000317 unsigned Align =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000318 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
Nate Begemanae232e72005-11-06 09:00:38 +0000319 AI->getAlignment());
Chris Lattnera8217e32005-05-13 23:14:17 +0000320
Reid Spencerb83eb642006-10-20 07:07:24 +0000321 TySize *= CUI->getZExtValue(); // Get total allocated size.
Chris Lattnerd222f6a2005-10-18 22:14:06 +0000322 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Chris Lattner1c08c712005-01-07 07:47:53 +0000323 StaticAllocaMap[AI] =
Chris Lattner6266c182007-04-25 04:08:28 +0000324 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner1c08c712005-01-07 07:47:53 +0000325 }
326
Jeff Cohen2aeaf4e2005-10-01 03:57:14 +0000327 for (; BB != EB; ++BB)
328 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattner1c08c712005-01-07 07:47:53 +0000329 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
330 if (!isa<AllocaInst>(I) ||
331 !StaticAllocaMap.count(cast<AllocaInst>(I)))
332 InitializeRegForValue(I);
333
334 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
335 // also creates the initial PHI MachineInstrs, though none of the input
336 // operands are populated.
Jeff Cohen2aeaf4e2005-10-01 03:57:14 +0000337 for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000338 MachineBasicBlock *MBB = new MachineBasicBlock(BB);
339 MBBMap[BB] = MBB;
340 MF.getBasicBlockList().push_back(MBB);
341
342 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
343 // appropriate.
344 PHINode *PN;
Chris Lattner8c494ab2006-10-27 23:50:33 +0000345 for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){
346 if (PN->use_empty()) continue;
347
348 MVT::ValueType VT = TLI.getValueType(PN->getType());
Dan Gohman7f321562007-06-25 16:23:39 +0000349 unsigned NumRegisters = TLI.getNumRegisters(VT);
Chris Lattner8c494ab2006-10-27 23:50:33 +0000350 unsigned PHIReg = ValueMap[PN];
351 assert(PHIReg && "PHI node does not have an assigned virtual register!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000352 const TargetInstrInfo *TII = TLI.getTargetMachine().getInstrInfo();
Dan Gohmanb9f10192007-06-21 14:42:22 +0000353 for (unsigned i = 0; i != NumRegisters; ++i)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000354 BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i);
Chris Lattner8c494ab2006-10-27 23:50:33 +0000355 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000356 }
357}
358
Chris Lattner3c384492006-03-16 19:51:18 +0000359/// CreateRegForValue - Allocate the appropriate number of virtual registers of
360/// the correctly promoted or expanded types. Assign these registers
361/// consecutive vreg numbers and return the first assigned number.
362unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
Dan Gohman23ce5022008-04-25 18:27:55 +0000363 SmallVector<MVT::ValueType, 4> ValueVTs;
Chris Lattnerb606dba2008-04-28 06:44:42 +0000364 ComputeValueVTs(TLI, V->getType(), ValueVTs);
Bill Wendling95b39552007-04-24 21:13:23 +0000365
Dan Gohman23ce5022008-04-25 18:27:55 +0000366 unsigned FirstReg = 0;
Dan Gohmanb20d4f82008-04-28 17:42:03 +0000367 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Dan Gohman23ce5022008-04-25 18:27:55 +0000368 MVT::ValueType ValueVT = ValueVTs[Value];
Dan Gohman23ce5022008-04-25 18:27:55 +0000369 MVT::ValueType RegisterVT = TLI.getRegisterType(ValueVT);
Dan Gohman8c8c5fc2007-06-27 14:34:07 +0000370
Chris Lattnerb606dba2008-04-28 06:44:42 +0000371 unsigned NumRegs = TLI.getNumRegisters(ValueVT);
Dan Gohman23ce5022008-04-25 18:27:55 +0000372 for (unsigned i = 0; i != NumRegs; ++i) {
373 unsigned R = MakeReg(RegisterVT);
374 if (!FirstReg) FirstReg = R;
375 }
376 }
377 return FirstReg;
Chris Lattner3c384492006-03-16 19:51:18 +0000378}
Chris Lattner1c08c712005-01-07 07:47:53 +0000379
380//===----------------------------------------------------------------------===//
381/// SelectionDAGLowering - This is the common target-independent lowering
382/// implementation that is parameterized by a TargetLowering object.
383/// Also, targets can overload any lowering method.
384///
385namespace llvm {
386class SelectionDAGLowering {
387 MachineBasicBlock *CurMBB;
388
Chris Lattner0da331f2007-02-04 01:31:47 +0000389 DenseMap<const Value*, SDOperand> NodeMap;
Chris Lattner1c08c712005-01-07 07:47:53 +0000390
Chris Lattnerd3948112005-01-17 22:19:26 +0000391 /// PendingLoads - Loads are not emitted to the program immediately. We bunch
392 /// them up and then emit token factor nodes when possible. This allows us to
393 /// get simple disambiguation between loads without worrying about alias
394 /// analysis.
395 std::vector<SDOperand> PendingLoads;
396
Dan Gohman86e1ebf2008-03-27 19:56:19 +0000397 /// PendingExports - CopyToReg nodes that copy values to virtual registers
398 /// for export to other blocks need to be emitted before any terminator
399 /// instruction, but they have no other ordering requirements. We bunch them
400 /// up and the emit a single tokenfactor for them just before terminator
401 /// instructions.
402 std::vector<SDOperand> PendingExports;
403
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000404 /// Case - A struct to record the Value for a switch case, and the
405 /// case's target basic block.
406 struct Case {
407 Constant* Low;
408 Constant* High;
409 MachineBasicBlock* BB;
410
411 Case() : Low(0), High(0), BB(0) { }
412 Case(Constant* low, Constant* high, MachineBasicBlock* bb) :
413 Low(low), High(high), BB(bb) { }
414 uint64_t size() const {
415 uint64_t rHigh = cast<ConstantInt>(High)->getSExtValue();
416 uint64_t rLow = cast<ConstantInt>(Low)->getSExtValue();
417 return (rHigh - rLow + 1ULL);
418 }
419 };
420
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000421 struct CaseBits {
422 uint64_t Mask;
423 MachineBasicBlock* BB;
424 unsigned Bits;
425
426 CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits):
427 Mask(mask), BB(bb), Bits(bits) { }
428 };
429
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000430 typedef std::vector<Case> CaseVector;
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000431 typedef std::vector<CaseBits> CaseBitsVector;
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000432 typedef CaseVector::iterator CaseItr;
433 typedef std::pair<CaseItr, CaseItr> CaseRange;
Nate Begemanf15485a2006-03-27 01:32:24 +0000434
435 /// CaseRec - A struct with ctor used in lowering switches to a binary tree
436 /// of conditional branches.
437 struct CaseRec {
438 CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) :
439 CaseBB(bb), LT(lt), GE(ge), Range(r) {}
440
441 /// CaseBB - The MBB in which to emit the compare and branch
442 MachineBasicBlock *CaseBB;
443 /// LT, GE - If nonzero, we know the current case value must be less-than or
444 /// greater-than-or-equal-to these Constants.
445 Constant *LT;
446 Constant *GE;
447 /// Range - A pair of iterators representing the range of case values to be
448 /// processed at this point in the binary search tree.
449 CaseRange Range;
450 };
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +0000451
452 typedef std::vector<CaseRec> CaseRecVector;
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000453
454 /// The comparison function for sorting the switch case values in the vector.
455 /// WARNING: Case ranges should be disjoint!
Nate Begemanf15485a2006-03-27 01:32:24 +0000456 struct CaseCmp {
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000457 bool operator () (const Case& C1, const Case& C2) {
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000458 assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High));
459 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
460 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
461 return CI1->getValue().slt(CI2->getValue());
Nate Begemanf15485a2006-03-27 01:32:24 +0000462 }
463 };
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000464
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000465 struct CaseBitsCmp {
466 bool operator () (const CaseBits& C1, const CaseBits& C2) {
467 return C1.Bits > C2.Bits;
468 }
469 };
470
Anton Korobeynikov5502bf62007-04-04 21:14:49 +0000471 unsigned Clusterify(CaseVector& Cases, const SwitchInst &SI);
Nate Begemanf15485a2006-03-27 01:32:24 +0000472
Chris Lattner1c08c712005-01-07 07:47:53 +0000473public:
474 // TLI - This is information that describes the available target features we
475 // need for lowering. This indicates when operations are unavailable,
476 // implemented with a libcall, etc.
477 TargetLowering &TLI;
478 SelectionDAG &DAG;
Owen Andersona69571c2006-05-03 01:29:57 +0000479 const TargetData *TD;
Dan Gohman5f43f922007-08-27 16:26:13 +0000480 AliasAnalysis &AA;
Chris Lattner1c08c712005-01-07 07:47:53 +0000481
Nate Begemanf15485a2006-03-27 01:32:24 +0000482 /// SwitchCases - Vector of CaseBlock structures used to communicate
483 /// SwitchInst code generation information.
484 std::vector<SelectionDAGISel::CaseBlock> SwitchCases;
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +0000485 /// JTCases - Vector of JumpTable structures used to communicate
486 /// SwitchInst code generation information.
487 std::vector<SelectionDAGISel::JumpTableBlock> JTCases;
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000488 std::vector<SelectionDAGISel::BitTestBlock> BitTestCases;
Nate Begemanf15485a2006-03-27 01:32:24 +0000489
Chris Lattner1c08c712005-01-07 07:47:53 +0000490 /// FuncInfo - Information about the function as a whole.
491 ///
492 FunctionLoweringInfo &FuncInfo;
Gordon Henriksence224772008-01-07 01:30:38 +0000493
494 /// GCI - Garbage collection metadata for the function.
495 CollectorMetadata *GCI;
Chris Lattner1c08c712005-01-07 07:47:53 +0000496
497 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
Dan Gohman5f43f922007-08-27 16:26:13 +0000498 AliasAnalysis &aa,
Gordon Henriksence224772008-01-07 01:30:38 +0000499 FunctionLoweringInfo &funcinfo,
500 CollectorMetadata *gci)
Dan Gohman5f43f922007-08-27 16:26:13 +0000501 : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), AA(aa),
Gordon Henriksence224772008-01-07 01:30:38 +0000502 FuncInfo(funcinfo), GCI(gci) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000503 }
504
Dan Gohman86e1ebf2008-03-27 19:56:19 +0000505 /// getRoot - Return the current virtual root of the Selection DAG,
506 /// flushing any PendingLoad items. This must be done before emitting
507 /// a store or any other node that may need to be ordered after any
508 /// prior load instructions.
Chris Lattnera651cf62005-01-17 19:43:36 +0000509 ///
510 SDOperand getRoot() {
Chris Lattnerd3948112005-01-17 22:19:26 +0000511 if (PendingLoads.empty())
512 return DAG.getRoot();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000513
Chris Lattnerd3948112005-01-17 22:19:26 +0000514 if (PendingLoads.size() == 1) {
515 SDOperand Root = PendingLoads[0];
516 DAG.setRoot(Root);
517 PendingLoads.clear();
518 return Root;
519 }
520
521 // Otherwise, we have to make a token factor node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000522 SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
523 &PendingLoads[0], PendingLoads.size());
Chris Lattnerd3948112005-01-17 22:19:26 +0000524 PendingLoads.clear();
525 DAG.setRoot(Root);
526 return Root;
Chris Lattnera651cf62005-01-17 19:43:36 +0000527 }
528
Dan Gohman86e1ebf2008-03-27 19:56:19 +0000529 /// getControlRoot - Similar to getRoot, but instead of flushing all the
530 /// PendingLoad items, flush all the PendingExports items. It is necessary
531 /// to do this before emitting a terminator instruction.
532 ///
533 SDOperand getControlRoot() {
534 SDOperand Root = DAG.getRoot();
535
536 if (PendingExports.empty())
537 return Root;
538
539 // Turn all of the CopyToReg chains into one factored node.
540 if (Root.getOpcode() != ISD::EntryToken) {
541 unsigned i = 0, e = PendingExports.size();
542 for (; i != e; ++i) {
543 assert(PendingExports[i].Val->getNumOperands() > 1);
544 if (PendingExports[i].Val->getOperand(0) == Root)
545 break; // Don't add the root if we already indirectly depend on it.
546 }
547
548 if (i == e)
549 PendingExports.push_back(Root);
550 }
551
552 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
553 &PendingExports[0],
554 PendingExports.size());
555 PendingExports.clear();
556 DAG.setRoot(Root);
557 return Root;
558 }
559
560 void CopyValueToVirtualRegister(Value *V, unsigned Reg);
Chris Lattner571e4342006-10-27 21:36:01 +0000561
Chris Lattner1c08c712005-01-07 07:47:53 +0000562 void visit(Instruction &I) { visit(I.getOpcode(), I); }
563
564 void visit(unsigned Opcode, User &I) {
Chris Lattner1e7aa5c2006-11-10 04:41:34 +0000565 // Note: this doesn't use InstVisitor, because it has to work with
566 // ConstantExpr's in addition to instructions.
Chris Lattner1c08c712005-01-07 07:47:53 +0000567 switch (Opcode) {
568 default: assert(0 && "Unknown instruction type encountered!");
569 abort();
570 // Build the switch statement using the Instruction.def file.
571#define HANDLE_INST(NUM, OPCODE, CLASS) \
572 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
573#include "llvm/Instruction.def"
574 }
575 }
576
577 void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
578
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000579 SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +0000580 const Value *SV, SDOperand Root,
Christopher Lamb95c218a2007-04-22 23:15:30 +0000581 bool isVolatile, unsigned Alignment);
Chris Lattner1c08c712005-01-07 07:47:53 +0000582
Chris Lattner199862b2006-03-16 19:57:50 +0000583 SDOperand getValue(const Value *V);
Chris Lattner1c08c712005-01-07 07:47:53 +0000584
Chris Lattner0da331f2007-02-04 01:31:47 +0000585 void setValue(const Value *V, SDOperand NewN) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000586 SDOperand &N = NodeMap[V];
587 assert(N.Val == 0 && "Already set a value for this node!");
Chris Lattner0da331f2007-02-04 01:31:47 +0000588 N = NewN;
Chris Lattner1c08c712005-01-07 07:47:53 +0000589 }
Chris Lattner4e4b5762006-02-01 18:59:47 +0000590
Evan Cheng5c807602008-02-26 02:33:44 +0000591 void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, bool HasEarlyClobber,
Chris Lattnere7cf56a2007-04-30 21:11:17 +0000592 std::set<unsigned> &OutputRegs,
593 std::set<unsigned> &InputRegs);
Nate Begemanf15485a2006-03-27 01:32:24 +0000594
Chris Lattner571e4342006-10-27 21:36:01 +0000595 void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB,
596 MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
597 unsigned Opc);
Chris Lattner8c494ab2006-10-27 23:50:33 +0000598 bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB);
Chris Lattner571e4342006-10-27 21:36:01 +0000599 void ExportFromCurrentBlock(Value *V);
Duncan Sands6f74b482007-12-19 09:48:52 +0000600 void LowerCallTo(CallSite CS, SDOperand Callee, bool IsTailCall,
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000601 MachineBasicBlock *LandingPad = NULL);
Duncan Sandsdc024672007-11-27 13:23:08 +0000602
Chris Lattner1c08c712005-01-07 07:47:53 +0000603 // Terminator instructions.
604 void visitRet(ReturnInst &I);
605 void visitBr(BranchInst &I);
Nate Begemanf15485a2006-03-27 01:32:24 +0000606 void visitSwitch(SwitchInst &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000607 void visitUnreachable(UnreachableInst &I) { /* noop */ }
608
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +0000609 // Helpers for visitSwitch
Anton Korobeynikovdd433212007-03-27 12:05:48 +0000610 bool handleSmallSwitchRange(CaseRec& CR,
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +0000611 CaseRecVector& WorkList,
612 Value* SV,
613 MachineBasicBlock* Default);
Anton Korobeynikovdd433212007-03-27 12:05:48 +0000614 bool handleJTSwitchCase(CaseRec& CR,
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +0000615 CaseRecVector& WorkList,
616 Value* SV,
617 MachineBasicBlock* Default);
Anton Korobeynikovdd433212007-03-27 12:05:48 +0000618 bool handleBTSplitSwitchCase(CaseRec& CR,
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +0000619 CaseRecVector& WorkList,
620 Value* SV,
621 MachineBasicBlock* Default);
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000622 bool handleBitTestsSwitchCase(CaseRec& CR,
623 CaseRecVector& WorkList,
624 Value* SV,
625 MachineBasicBlock* Default);
Nate Begemanf15485a2006-03-27 01:32:24 +0000626 void visitSwitchCase(SelectionDAGISel::CaseBlock &CB);
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000627 void visitBitTestHeader(SelectionDAGISel::BitTestBlock &B);
628 void visitBitTestCase(MachineBasicBlock* NextMBB,
629 unsigned Reg,
630 SelectionDAGISel::BitTestCase &B);
Nate Begeman37efe672006-04-22 18:53:45 +0000631 void visitJumpTable(SelectionDAGISel::JumpTable &JT);
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +0000632 void visitJumpTableHeader(SelectionDAGISel::JumpTable &JT,
633 SelectionDAGISel::JumpTableHeader &JTH);
Nate Begemanf15485a2006-03-27 01:32:24 +0000634
Chris Lattner1c08c712005-01-07 07:47:53 +0000635 // These all get lowered before this pass.
Jim Laskeyb180aa12007-02-21 22:53:45 +0000636 void visitInvoke(InvokeInst &I);
637 void visitUnwind(UnwindInst &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000638
Dan Gohman7f321562007-06-25 16:23:39 +0000639 void visitBinary(User &I, unsigned OpCode);
Nate Begemane21ea612005-11-18 07:42:56 +0000640 void visitShift(User &I, unsigned Opcode);
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000641 void visitAdd(User &I) {
Dan Gohman7f321562007-06-25 16:23:39 +0000642 if (I.getType()->isFPOrFPVector())
643 visitBinary(I, ISD::FADD);
Reid Spencer1628cec2006-10-26 06:15:43 +0000644 else
Dan Gohman7f321562007-06-25 16:23:39 +0000645 visitBinary(I, ISD::ADD);
Chris Lattner01b3d732005-09-28 22:28:18 +0000646 }
Chris Lattnerb9fccc42005-04-02 05:04:50 +0000647 void visitSub(User &I);
Reid Spencer1628cec2006-10-26 06:15:43 +0000648 void visitMul(User &I) {
Dan Gohman7f321562007-06-25 16:23:39 +0000649 if (I.getType()->isFPOrFPVector())
650 visitBinary(I, ISD::FMUL);
Reid Spencer1628cec2006-10-26 06:15:43 +0000651 else
Dan Gohman7f321562007-06-25 16:23:39 +0000652 visitBinary(I, ISD::MUL);
Chris Lattner01b3d732005-09-28 22:28:18 +0000653 }
Dan Gohman7f321562007-06-25 16:23:39 +0000654 void visitURem(User &I) { visitBinary(I, ISD::UREM); }
655 void visitSRem(User &I) { visitBinary(I, ISD::SREM); }
656 void visitFRem(User &I) { visitBinary(I, ISD::FREM); }
657 void visitUDiv(User &I) { visitBinary(I, ISD::UDIV); }
658 void visitSDiv(User &I) { visitBinary(I, ISD::SDIV); }
659 void visitFDiv(User &I) { visitBinary(I, ISD::FDIV); }
660 void visitAnd (User &I) { visitBinary(I, ISD::AND); }
661 void visitOr (User &I) { visitBinary(I, ISD::OR); }
662 void visitXor (User &I) { visitBinary(I, ISD::XOR); }
Reid Spencer24d6da52007-01-21 00:29:26 +0000663 void visitShl (User &I) { visitShift(I, ISD::SHL); }
Reid Spencer3822ff52006-11-08 06:47:33 +0000664 void visitLShr(User &I) { visitShift(I, ISD::SRL); }
665 void visitAShr(User &I) { visitShift(I, ISD::SRA); }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000666 void visitICmp(User &I);
667 void visitFCmp(User &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000668 // Visit the conversion instructions
669 void visitTrunc(User &I);
670 void visitZExt(User &I);
671 void visitSExt(User &I);
672 void visitFPTrunc(User &I);
673 void visitFPExt(User &I);
674 void visitFPToUI(User &I);
675 void visitFPToSI(User &I);
676 void visitUIToFP(User &I);
677 void visitSIToFP(User &I);
678 void visitPtrToInt(User &I);
679 void visitIntToPtr(User &I);
680 void visitBitCast(User &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000681
Chris Lattner2bbd8102006-03-29 00:11:43 +0000682 void visitExtractElement(User &I);
683 void visitInsertElement(User &I);
Chris Lattner3e104b12006-04-08 04:15:24 +0000684 void visitShuffleVector(User &I);
Chris Lattnerc7029802006-03-18 01:44:44 +0000685
Chris Lattner1c08c712005-01-07 07:47:53 +0000686 void visitGetElementPtr(User &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000687 void visitSelect(User &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000688
689 void visitMalloc(MallocInst &I);
690 void visitFree(FreeInst &I);
691 void visitAlloca(AllocaInst &I);
692 void visitLoad(LoadInst &I);
693 void visitStore(StoreInst &I);
694 void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
695 void visitCall(CallInst &I);
Duncan Sandsfd7b3262007-12-17 18:08:19 +0000696 void visitInlineAsm(CallSite CS);
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +0000697 const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic);
Chris Lattner0eade312006-03-24 02:22:33 +0000698 void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic);
Chris Lattner1c08c712005-01-07 07:47:53 +0000699
Chris Lattner1c08c712005-01-07 07:47:53 +0000700 void visitVAStart(CallInst &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000701 void visitVAArg(VAArgInst &I);
702 void visitVAEnd(CallInst &I);
703 void visitVACopy(CallInst &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000704
Dan Gohmanef5d1942008-03-11 21:11:25 +0000705 void visitGetResult(GetResultInst &I);
Devang Patel40a04212008-02-19 22:15:16 +0000706
Chris Lattner1c08c712005-01-07 07:47:53 +0000707 void visitUserOp1(Instruction &I) {
708 assert(0 && "UserOp1 should not exist at instruction selection time!");
709 abort();
710 }
711 void visitUserOp2(Instruction &I) {
712 assert(0 && "UserOp2 should not exist at instruction selection time!");
713 abort();
714 }
715};
716} // end namespace llvm
717
Dan Gohman6183f782007-07-05 20:12:34 +0000718
Duncan Sandsb988bac2008-02-11 20:58:28 +0000719/// getCopyFromParts - Create a value that contains the specified legal parts
720/// combined into the value they represent. If the parts combine to a type
721/// larger then ValueVT then AssertOp can be used to specify whether the extra
722/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
Chris Lattner4468c1f2008-03-09 09:38:46 +0000723/// (ISD::AssertSext).
Dan Gohman6183f782007-07-05 20:12:34 +0000724static SDOperand getCopyFromParts(SelectionDAG &DAG,
725 const SDOperand *Parts,
726 unsigned NumParts,
727 MVT::ValueType PartVT,
728 MVT::ValueType ValueVT,
Chris Lattner4468c1f2008-03-09 09:38:46 +0000729 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Duncan Sands014e04a2008-02-12 20:46:31 +0000730 assert(NumParts > 0 && "No parts to assemble!");
731 TargetLowering &TLI = DAG.getTargetLoweringInfo();
732 SDOperand Val = Parts[0];
Dan Gohman6183f782007-07-05 20:12:34 +0000733
Duncan Sands014e04a2008-02-12 20:46:31 +0000734 if (NumParts > 1) {
735 // Assemble the value from multiple parts.
736 if (!MVT::isVector(ValueVT)) {
737 unsigned PartBits = MVT::getSizeInBits(PartVT);
738 unsigned ValueBits = MVT::getSizeInBits(ValueVT);
Dan Gohman6183f782007-07-05 20:12:34 +0000739
Duncan Sands014e04a2008-02-12 20:46:31 +0000740 // Assemble the power of 2 part.
741 unsigned RoundParts = NumParts & (NumParts - 1) ?
742 1 << Log2_32(NumParts) : NumParts;
743 unsigned RoundBits = PartBits * RoundParts;
744 MVT::ValueType RoundVT = RoundBits == ValueBits ?
745 ValueVT : MVT::getIntegerType(RoundBits);
746 SDOperand Lo, Hi;
747
748 if (RoundParts > 2) {
749 MVT::ValueType HalfVT = MVT::getIntegerType(RoundBits/2);
750 Lo = getCopyFromParts(DAG, Parts, RoundParts/2, PartVT, HalfVT);
751 Hi = getCopyFromParts(DAG, Parts+RoundParts/2, RoundParts/2,
752 PartVT, HalfVT);
Dan Gohman6183f782007-07-05 20:12:34 +0000753 } else {
Duncan Sands014e04a2008-02-12 20:46:31 +0000754 Lo = Parts[0];
755 Hi = Parts[1];
Dan Gohman6183f782007-07-05 20:12:34 +0000756 }
Duncan Sands014e04a2008-02-12 20:46:31 +0000757 if (TLI.isBigEndian())
758 std::swap(Lo, Hi);
759 Val = DAG.getNode(ISD::BUILD_PAIR, RoundVT, Lo, Hi);
760
761 if (RoundParts < NumParts) {
762 // Assemble the trailing non-power-of-2 part.
763 unsigned OddParts = NumParts - RoundParts;
764 MVT::ValueType OddVT = MVT::getIntegerType(OddParts * PartBits);
765 Hi = getCopyFromParts(DAG, Parts+RoundParts, OddParts, PartVT, OddVT);
766
767 // Combine the round and odd parts.
768 Lo = Val;
769 if (TLI.isBigEndian())
770 std::swap(Lo, Hi);
771 MVT::ValueType TotalVT = MVT::getIntegerType(NumParts * PartBits);
772 Hi = DAG.getNode(ISD::ANY_EXTEND, TotalVT, Hi);
773 Hi = DAG.getNode(ISD::SHL, TotalVT, Hi,
774 DAG.getConstant(MVT::getSizeInBits(Lo.getValueType()),
775 TLI.getShiftAmountTy()));
776 Lo = DAG.getNode(ISD::ZERO_EXTEND, TotalVT, Lo);
777 Val = DAG.getNode(ISD::OR, TotalVT, Lo, Hi);
778 }
779 } else {
780 // Handle a multi-element vector.
781 MVT::ValueType IntermediateVT, RegisterVT;
782 unsigned NumIntermediates;
783 unsigned NumRegs =
784 TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
785 RegisterVT);
786
787 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
788 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
789 assert(RegisterVT == Parts[0].getValueType() &&
790 "Part type doesn't match part!");
791
792 // Assemble the parts into intermediate operands.
793 SmallVector<SDOperand, 8> Ops(NumIntermediates);
794 if (NumIntermediates == NumParts) {
795 // If the register was not expanded, truncate or copy the value,
796 // as appropriate.
797 for (unsigned i = 0; i != NumParts; ++i)
798 Ops[i] = getCopyFromParts(DAG, &Parts[i], 1,
799 PartVT, IntermediateVT);
800 } else if (NumParts > 0) {
801 // If the intermediate type was expanded, build the intermediate operands
802 // from the parts.
803 assert(NumParts % NumIntermediates == 0 &&
804 "Must expand into a divisible number of parts!");
805 unsigned Factor = NumParts / NumIntermediates;
806 for (unsigned i = 0; i != NumIntermediates; ++i)
807 Ops[i] = getCopyFromParts(DAG, &Parts[i * Factor], Factor,
808 PartVT, IntermediateVT);
809 }
810
811 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
812 // operands.
813 Val = DAG.getNode(MVT::isVector(IntermediateVT) ?
814 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR,
815 ValueVT, &Ops[0], NumIntermediates);
Dan Gohman6183f782007-07-05 20:12:34 +0000816 }
Dan Gohman6183f782007-07-05 20:12:34 +0000817 }
818
Duncan Sands014e04a2008-02-12 20:46:31 +0000819 // There is now one part, held in Val. Correct it to match ValueVT.
820 PartVT = Val.getValueType();
Dan Gohman6183f782007-07-05 20:12:34 +0000821
Duncan Sands014e04a2008-02-12 20:46:31 +0000822 if (PartVT == ValueVT)
823 return Val;
Dan Gohman6183f782007-07-05 20:12:34 +0000824
Duncan Sands014e04a2008-02-12 20:46:31 +0000825 if (MVT::isVector(PartVT)) {
826 assert(MVT::isVector(ValueVT) && "Unknown vector conversion!");
827 return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
Dan Gohman6183f782007-07-05 20:12:34 +0000828 }
Duncan Sands014e04a2008-02-12 20:46:31 +0000829
830 if (MVT::isVector(ValueVT)) {
831 assert(MVT::getVectorElementType(ValueVT) == PartVT &&
832 MVT::getVectorNumElements(ValueVT) == 1 &&
833 "Only trivial scalar-to-vector conversions should get here!");
834 return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val);
835 }
836
837 if (MVT::isInteger(PartVT) &&
838 MVT::isInteger(ValueVT)) {
839 if (MVT::getSizeInBits(ValueVT) < MVT::getSizeInBits(PartVT)) {
840 // For a truncate, see if we have any information to
841 // indicate whether the truncated bits will always be
842 // zero or sign-extension.
843 if (AssertOp != ISD::DELETED_NODE)
844 Val = DAG.getNode(AssertOp, PartVT, Val,
845 DAG.getValueType(ValueVT));
846 return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
847 } else {
848 return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val);
849 }
850 }
851
Chris Lattnerd43d85c2008-03-09 07:47:22 +0000852 if (MVT::isFloatingPoint(PartVT) && MVT::isFloatingPoint(ValueVT)) {
853 if (ValueVT < Val.getValueType())
Chris Lattner4468c1f2008-03-09 09:38:46 +0000854 // FP_ROUND's are always exact here.
Chris Lattnerd43d85c2008-03-09 07:47:22 +0000855 return DAG.getNode(ISD::FP_ROUND, ValueVT, Val,
Chris Lattner4468c1f2008-03-09 09:38:46 +0000856 DAG.getIntPtrConstant(1));
Chris Lattnerd43d85c2008-03-09 07:47:22 +0000857 return DAG.getNode(ISD::FP_EXTEND, ValueVT, Val);
858 }
Duncan Sands014e04a2008-02-12 20:46:31 +0000859
860 if (MVT::getSizeInBits(PartVT) == MVT::getSizeInBits(ValueVT))
861 return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
862
863 assert(0 && "Unknown mismatch!");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000864 return SDOperand();
Dan Gohman6183f782007-07-05 20:12:34 +0000865}
866
Duncan Sandsb988bac2008-02-11 20:58:28 +0000867/// getCopyToParts - Create a series of nodes that contain the specified value
868/// split into legal parts. If the parts contain more bits than Val, then, for
869/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dan Gohman6183f782007-07-05 20:12:34 +0000870static void getCopyToParts(SelectionDAG &DAG,
871 SDOperand Val,
872 SDOperand *Parts,
873 unsigned NumParts,
Duncan Sandsb988bac2008-02-11 20:58:28 +0000874 MVT::ValueType PartVT,
875 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohman25ac7e82007-08-10 14:59:38 +0000876 TargetLowering &TLI = DAG.getTargetLoweringInfo();
877 MVT::ValueType PtrVT = TLI.getPointerTy();
Dan Gohman6183f782007-07-05 20:12:34 +0000878 MVT::ValueType ValueVT = Val.getValueType();
Duncan Sands014e04a2008-02-12 20:46:31 +0000879 unsigned PartBits = MVT::getSizeInBits(PartVT);
880 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
Dan Gohman6183f782007-07-05 20:12:34 +0000881
Duncan Sands014e04a2008-02-12 20:46:31 +0000882 if (!NumParts)
883 return;
884
885 if (!MVT::isVector(ValueVT)) {
886 if (PartVT == ValueVT) {
887 assert(NumParts == 1 && "No-op copy with multiple parts!");
888 Parts[0] = Val;
Dan Gohman6183f782007-07-05 20:12:34 +0000889 return;
890 }
891
Duncan Sands014e04a2008-02-12 20:46:31 +0000892 if (NumParts * PartBits > MVT::getSizeInBits(ValueVT)) {
893 // If the parts cover more bits than the value has, promote the value.
894 if (MVT::isFloatingPoint(PartVT) && MVT::isFloatingPoint(ValueVT)) {
895 assert(NumParts == 1 && "Do not know what to promote to!");
Dan Gohman6183f782007-07-05 20:12:34 +0000896 Val = DAG.getNode(ISD::FP_EXTEND, PartVT, Val);
Duncan Sands014e04a2008-02-12 20:46:31 +0000897 } else if (MVT::isInteger(PartVT) && MVT::isInteger(ValueVT)) {
898 ValueVT = MVT::getIntegerType(NumParts * PartBits);
899 Val = DAG.getNode(ExtendKind, ValueVT, Val);
900 } else {
901 assert(0 && "Unknown mismatch!");
902 }
903 } else if (PartBits == MVT::getSizeInBits(ValueVT)) {
904 // Different types of the same size.
905 assert(NumParts == 1 && PartVT != ValueVT);
906 Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
907 } else if (NumParts * PartBits < MVT::getSizeInBits(ValueVT)) {
908 // If the parts cover less bits than value has, truncate the value.
909 if (MVT::isInteger(PartVT) && MVT::isInteger(ValueVT)) {
910 ValueVT = MVT::getIntegerType(NumParts * PartBits);
911 Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
Dan Gohman6183f782007-07-05 20:12:34 +0000912 } else {
913 assert(0 && "Unknown mismatch!");
914 }
915 }
Duncan Sands014e04a2008-02-12 20:46:31 +0000916
917 // The value may have changed - recompute ValueVT.
918 ValueVT = Val.getValueType();
919 assert(NumParts * PartBits == MVT::getSizeInBits(ValueVT) &&
920 "Failed to tile the value with PartVT!");
921
922 if (NumParts == 1) {
923 assert(PartVT == ValueVT && "Type conversion failed!");
924 Parts[0] = Val;
925 return;
926 }
927
928 // Expand the value into multiple parts.
929 if (NumParts & (NumParts - 1)) {
930 // The number of parts is not a power of 2. Split off and copy the tail.
931 assert(MVT::isInteger(PartVT) && MVT::isInteger(ValueVT) &&
932 "Do not know what to expand to!");
933 unsigned RoundParts = 1 << Log2_32(NumParts);
934 unsigned RoundBits = RoundParts * PartBits;
935 unsigned OddParts = NumParts - RoundParts;
936 SDOperand OddVal = DAG.getNode(ISD::SRL, ValueVT, Val,
937 DAG.getConstant(RoundBits,
938 TLI.getShiftAmountTy()));
939 getCopyToParts(DAG, OddVal, Parts + RoundParts, OddParts, PartVT);
940 if (TLI.isBigEndian())
941 // The odd parts were reversed by getCopyToParts - unreverse them.
942 std::reverse(Parts + RoundParts, Parts + NumParts);
943 NumParts = RoundParts;
944 ValueVT = MVT::getIntegerType(NumParts * PartBits);
945 Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
946 }
947
948 // The number of parts is a power of 2. Repeatedly bisect the value using
949 // EXTRACT_ELEMENT.
Duncan Sands25eb0432008-03-12 20:30:08 +0000950 Parts[0] = DAG.getNode(ISD::BIT_CONVERT,
951 MVT::getIntegerType(MVT::getSizeInBits(ValueVT)),
952 Val);
Duncan Sands014e04a2008-02-12 20:46:31 +0000953 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
954 for (unsigned i = 0; i < NumParts; i += StepSize) {
955 unsigned ThisBits = StepSize * PartBits / 2;
Duncan Sands25eb0432008-03-12 20:30:08 +0000956 MVT::ValueType ThisVT = MVT::getIntegerType (ThisBits);
957 SDOperand &Part0 = Parts[i];
958 SDOperand &Part1 = Parts[i+StepSize/2];
Duncan Sands014e04a2008-02-12 20:46:31 +0000959
Duncan Sands25eb0432008-03-12 20:30:08 +0000960 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0,
961 DAG.getConstant(1, PtrVT));
962 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0,
963 DAG.getConstant(0, PtrVT));
964
965 if (ThisBits == PartBits && ThisVT != PartVT) {
966 Part0 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part0);
967 Part1 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part1);
968 }
Duncan Sands014e04a2008-02-12 20:46:31 +0000969 }
970 }
971
972 if (TLI.isBigEndian())
973 std::reverse(Parts, Parts + NumParts);
974
975 return;
976 }
977
978 // Vector ValueVT.
979 if (NumParts == 1) {
980 if (PartVT != ValueVT) {
981 if (MVT::isVector(PartVT)) {
982 Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
983 } else {
984 assert(MVT::getVectorElementType(ValueVT) == PartVT &&
985 MVT::getVectorNumElements(ValueVT) == 1 &&
986 "Only trivial vector-to-scalar conversions should get here!");
987 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val,
988 DAG.getConstant(0, PtrVT));
989 }
990 }
991
Dan Gohman6183f782007-07-05 20:12:34 +0000992 Parts[0] = Val;
993 return;
994 }
995
996 // Handle a multi-element vector.
997 MVT::ValueType IntermediateVT, RegisterVT;
998 unsigned NumIntermediates;
999 unsigned NumRegs =
1000 DAG.getTargetLoweringInfo()
1001 .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
1002 RegisterVT);
1003 unsigned NumElements = MVT::getVectorNumElements(ValueVT);
1004
1005 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
1006 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
1007
1008 // Split the vector into intermediate operands.
1009 SmallVector<SDOperand, 8> Ops(NumIntermediates);
1010 for (unsigned i = 0; i != NumIntermediates; ++i)
1011 if (MVT::isVector(IntermediateVT))
1012 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR,
1013 IntermediateVT, Val,
1014 DAG.getConstant(i * (NumElements / NumIntermediates),
Dan Gohman25ac7e82007-08-10 14:59:38 +00001015 PtrVT));
Dan Gohman6183f782007-07-05 20:12:34 +00001016 else
1017 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
1018 IntermediateVT, Val,
Dan Gohman25ac7e82007-08-10 14:59:38 +00001019 DAG.getConstant(i, PtrVT));
Dan Gohman6183f782007-07-05 20:12:34 +00001020
1021 // Split the intermediate operands into legal parts.
1022 if (NumParts == NumIntermediates) {
1023 // If the register was not expanded, promote or copy the value,
1024 // as appropriate.
1025 for (unsigned i = 0; i != NumParts; ++i)
Dan Gohman532dc2e2007-07-09 20:59:04 +00001026 getCopyToParts(DAG, Ops[i], &Parts[i], 1, PartVT);
Dan Gohman6183f782007-07-05 20:12:34 +00001027 } else if (NumParts > 0) {
1028 // If the intermediate type was expanded, split each the value into
1029 // legal parts.
1030 assert(NumParts % NumIntermediates == 0 &&
1031 "Must expand into a divisible number of parts!");
1032 unsigned Factor = NumParts / NumIntermediates;
1033 for (unsigned i = 0; i != NumIntermediates; ++i)
Dan Gohman532dc2e2007-07-09 20:59:04 +00001034 getCopyToParts(DAG, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohman6183f782007-07-05 20:12:34 +00001035 }
1036}
1037
1038
Chris Lattner199862b2006-03-16 19:57:50 +00001039SDOperand SelectionDAGLowering::getValue(const Value *V) {
1040 SDOperand &N = NodeMap[V];
1041 if (N.Val) return N;
1042
Chris Lattner199862b2006-03-16 19:57:50 +00001043 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Chris Lattner6833b062008-04-28 07:16:35 +00001044 MVT::ValueType VT = TLI.getValueType(V->getType(), true);
Chris Lattnerb606dba2008-04-28 06:44:42 +00001045
1046 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1047 return N = DAG.getConstant(CI->getValue(), VT);
1048
1049 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattner199862b2006-03-16 19:57:50 +00001050 return N = DAG.getGlobalAddress(GV, VT);
Chris Lattnerb606dba2008-04-28 06:44:42 +00001051
1052 if (isa<ConstantPointerNull>(C))
Chris Lattner199862b2006-03-16 19:57:50 +00001053 return N = DAG.getConstant(0, TLI.getPointerTy());
Chris Lattnerb606dba2008-04-28 06:44:42 +00001054
1055 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1056 return N = DAG.getConstantFP(CFP->getValueAPF(), VT);
1057
Chris Lattner6833b062008-04-28 07:16:35 +00001058 if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()))
1059 return N = DAG.getNode(ISD::UNDEF, VT);
Chris Lattnerb606dba2008-04-28 06:44:42 +00001060
1061 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1062 visit(CE->getOpcode(), *CE);
1063 SDOperand N1 = NodeMap[V];
1064 assert(N1.Val && "visit didn't populate the ValueMap!");
1065 return N1;
1066 }
1067
Chris Lattner6833b062008-04-28 07:16:35 +00001068 const VectorType *VecTy = cast<VectorType>(V->getType());
Chris Lattnerb606dba2008-04-28 06:44:42 +00001069 unsigned NumElements = VecTy->getNumElements();
Chris Lattnerb606dba2008-04-28 06:44:42 +00001070
Chris Lattner6833b062008-04-28 07:16:35 +00001071 // Now that we know the number and type of the elements, get that number of
1072 // elements into the Ops array based on what kind of constant it is.
1073 SmallVector<SDOperand, 16> Ops;
Chris Lattnerb606dba2008-04-28 06:44:42 +00001074 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
1075 for (unsigned i = 0; i != NumElements; ++i)
1076 Ops.push_back(getValue(CP->getOperand(i)));
1077 } else {
Chris Lattner6833b062008-04-28 07:16:35 +00001078 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1079 "Unknown vector constant!");
1080 MVT::ValueType EltVT = TLI.getValueType(VecTy->getElementType());
1081
Chris Lattnerb606dba2008-04-28 06:44:42 +00001082 SDOperand Op;
Chris Lattner6833b062008-04-28 07:16:35 +00001083 if (isa<UndefValue>(C))
1084 Op = DAG.getNode(ISD::UNDEF, EltVT);
1085 else if (MVT::isFloatingPoint(EltVT))
1086 Op = DAG.getConstantFP(0, EltVT);
Chris Lattnerb606dba2008-04-28 06:44:42 +00001087 else
Chris Lattner6833b062008-04-28 07:16:35 +00001088 Op = DAG.getConstant(0, EltVT);
Chris Lattnerb606dba2008-04-28 06:44:42 +00001089 Ops.assign(NumElements, Op);
1090 }
1091
1092 // Create a BUILD_VECTOR node.
1093 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner199862b2006-03-16 19:57:50 +00001094 }
1095
Chris Lattnerb606dba2008-04-28 06:44:42 +00001096 // If this is a static alloca, generate it as the frameindex instead of
1097 // computation.
Chris Lattner199862b2006-03-16 19:57:50 +00001098 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1099 std::map<const AllocaInst*, int>::iterator SI =
Chris Lattnerb606dba2008-04-28 06:44:42 +00001100 FuncInfo.StaticAllocaMap.find(AI);
Chris Lattner199862b2006-03-16 19:57:50 +00001101 if (SI != FuncInfo.StaticAllocaMap.end())
1102 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
1103 }
1104
Chris Lattner251db182007-02-25 18:40:32 +00001105 unsigned InReg = FuncInfo.ValueMap[V];
1106 assert(InReg && "Value not in map!");
Chris Lattner199862b2006-03-16 19:57:50 +00001107
Chris Lattner6833b062008-04-28 07:16:35 +00001108 RegsForValue RFV(TLI, InReg, V->getType());
Dan Gohmanb6f5b002007-06-28 23:29:44 +00001109 SDOperand Chain = DAG.getEntryNode();
Dan Gohmanb6f5b002007-06-28 23:29:44 +00001110 return RFV.getCopyFromRegs(DAG, Chain, NULL);
Chris Lattner199862b2006-03-16 19:57:50 +00001111}
1112
1113
Chris Lattner1c08c712005-01-07 07:47:53 +00001114void SelectionDAGLowering::visitRet(ReturnInst &I) {
1115 if (I.getNumOperands() == 0) {
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001116 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getControlRoot()));
Chris Lattner1c08c712005-01-07 07:47:53 +00001117 return;
1118 }
Chris Lattnerb606dba2008-04-28 06:44:42 +00001119
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001120 SmallVector<SDOperand, 8> NewValues;
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001121 NewValues.push_back(getControlRoot());
1122 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Nate Begemanee625572006-01-27 21:09:22 +00001123 SDOperand RetOp = getValue(I.getOperand(i));
Duncan Sandsb988bac2008-02-11 20:58:28 +00001124 MVT::ValueType VT = RetOp.getValueType();
1125
Evan Cheng8e7d0562006-05-26 23:09:09 +00001126 // FIXME: C calling convention requires the return type to be promoted to
1127 // at least 32-bit. But this is not necessary for non-C calling conventions.
Duncan Sandsb988bac2008-02-11 20:58:28 +00001128 if (MVT::isInteger(VT)) {
1129 MVT::ValueType MinVT = TLI.getRegisterType(MVT::i32);
1130 if (MVT::getSizeInBits(VT) < MVT::getSizeInBits(MinVT))
1131 VT = MinVT;
1132 }
1133
1134 unsigned NumParts = TLI.getNumRegisters(VT);
1135 MVT::ValueType PartVT = TLI.getRegisterType(VT);
1136 SmallVector<SDOperand, 4> Parts(NumParts);
1137 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
1138
1139 const Function *F = I.getParent()->getParent();
1140 if (F->paramHasAttr(0, ParamAttr::SExt))
1141 ExtendKind = ISD::SIGN_EXTEND;
1142 else if (F->paramHasAttr(0, ParamAttr::ZExt))
1143 ExtendKind = ISD::ZERO_EXTEND;
1144
1145 getCopyToParts(DAG, RetOp, &Parts[0], NumParts, PartVT, ExtendKind);
1146
1147 for (unsigned i = 0; i < NumParts; ++i) {
1148 NewValues.push_back(Parts[i]);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001149 NewValues.push_back(DAG.getArgFlags(ISD::ArgFlagsTy()));
Nate Begemanee625572006-01-27 21:09:22 +00001150 }
Chris Lattner1c08c712005-01-07 07:47:53 +00001151 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001152 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other,
1153 &NewValues[0], NewValues.size()));
Chris Lattner1c08c712005-01-07 07:47:53 +00001154}
1155
Chris Lattner571e4342006-10-27 21:36:01 +00001156/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1157/// the current basic block, add it to ValueMap now so that we'll get a
1158/// CopyTo/FromReg.
1159void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
1160 // No need to export constants.
1161 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
1162
1163 // Already exported?
1164 if (FuncInfo.isExportedInst(V)) return;
1165
1166 unsigned Reg = FuncInfo.InitializeRegForValue(V);
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001167 CopyValueToVirtualRegister(V, Reg);
Chris Lattner571e4342006-10-27 21:36:01 +00001168}
1169
Chris Lattner8c494ab2006-10-27 23:50:33 +00001170bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
1171 const BasicBlock *FromBB) {
1172 // The operands of the setcc have to be in this block. We don't know
1173 // how to export them from some other block.
1174 if (Instruction *VI = dyn_cast<Instruction>(V)) {
1175 // Can export from current BB.
1176 if (VI->getParent() == FromBB)
1177 return true;
1178
1179 // Is already exported, noop.
1180 return FuncInfo.isExportedInst(V);
1181 }
1182
1183 // If this is an argument, we can export it if the BB is the entry block or
1184 // if it is already exported.
1185 if (isa<Argument>(V)) {
1186 if (FromBB == &FromBB->getParent()->getEntryBlock())
1187 return true;
1188
1189 // Otherwise, can only export this if it is already exported.
1190 return FuncInfo.isExportedInst(V);
1191 }
1192
1193 // Otherwise, constants can always be exported.
1194 return true;
1195}
1196
Chris Lattner6a586c82006-10-29 21:01:20 +00001197static bool InBlock(const Value *V, const BasicBlock *BB) {
1198 if (const Instruction *I = dyn_cast<Instruction>(V))
1199 return I->getParent() == BB;
1200 return true;
1201}
1202
Chris Lattner571e4342006-10-27 21:36:01 +00001203/// FindMergedConditions - If Cond is an expression like
1204void SelectionDAGLowering::FindMergedConditions(Value *Cond,
1205 MachineBasicBlock *TBB,
1206 MachineBasicBlock *FBB,
1207 MachineBasicBlock *CurBB,
1208 unsigned Opc) {
Chris Lattner571e4342006-10-27 21:36:01 +00001209 // If this node is not part of the or/and tree, emit it as a branch.
Reid Spencere4d87aa2006-12-23 06:05:41 +00001210 Instruction *BOp = dyn_cast<Instruction>(Cond);
Chris Lattner571e4342006-10-27 21:36:01 +00001211
Reid Spencere4d87aa2006-12-23 06:05:41 +00001212 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
1213 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
Chris Lattner6a586c82006-10-29 21:01:20 +00001214 BOp->getParent() != CurBB->getBasicBlock() ||
1215 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1216 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Chris Lattner571e4342006-10-27 21:36:01 +00001217 const BasicBlock *BB = CurBB->getBasicBlock();
1218
Reid Spencere4d87aa2006-12-23 06:05:41 +00001219 // If the leaf of the tree is a comparison, merge the condition into
1220 // the caseblock.
1221 if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) &&
1222 // The operands of the cmp have to be in this block. We don't know
Chris Lattner5a145f02006-10-29 18:23:37 +00001223 // how to export them from some other block. If this is the first block
1224 // of the sequence, no exporting is needed.
1225 (CurBB == CurMBB ||
1226 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1227 isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001228 BOp = cast<Instruction>(Cond);
1229 ISD::CondCode Condition;
1230 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
1231 switch (IC->getPredicate()) {
1232 default: assert(0 && "Unknown icmp predicate opcode!");
1233 case ICmpInst::ICMP_EQ: Condition = ISD::SETEQ; break;
1234 case ICmpInst::ICMP_NE: Condition = ISD::SETNE; break;
1235 case ICmpInst::ICMP_SLE: Condition = ISD::SETLE; break;
1236 case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break;
1237 case ICmpInst::ICMP_SGE: Condition = ISD::SETGE; break;
1238 case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break;
1239 case ICmpInst::ICMP_SLT: Condition = ISD::SETLT; break;
1240 case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break;
1241 case ICmpInst::ICMP_SGT: Condition = ISD::SETGT; break;
1242 case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break;
1243 }
1244 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
1245 ISD::CondCode FPC, FOC;
1246 switch (FC->getPredicate()) {
1247 default: assert(0 && "Unknown fcmp predicate opcode!");
1248 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1249 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1250 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1251 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1252 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1253 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1254 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1255 case FCmpInst::FCMP_ORD: FOC = ISD::SETEQ; FPC = ISD::SETO; break;
1256 case FCmpInst::FCMP_UNO: FOC = ISD::SETNE; FPC = ISD::SETUO; break;
1257 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1258 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1259 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1260 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1261 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1262 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1263 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1264 }
1265 if (FiniteOnlyFPMath())
1266 Condition = FOC;
1267 else
1268 Condition = FPC;
1269 } else {
Chris Lattner0da331f2007-02-04 01:31:47 +00001270 Condition = ISD::SETEQ; // silence warning.
Reid Spencere4d87aa2006-12-23 06:05:41 +00001271 assert(0 && "Unknown compare instruction");
Chris Lattner571e4342006-10-27 21:36:01 +00001272 }
1273
Chris Lattner571e4342006-10-27 21:36:01 +00001274 SelectionDAGISel::CaseBlock CB(Condition, BOp->getOperand(0),
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001275 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
Chris Lattner571e4342006-10-27 21:36:01 +00001276 SwitchCases.push_back(CB);
1277 return;
1278 }
1279
1280 // Create a CaseBlock record representing this branch.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001281 SelectionDAGISel::CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(),
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001282 NULL, TBB, FBB, CurBB);
Chris Lattner571e4342006-10-27 21:36:01 +00001283 SwitchCases.push_back(CB);
Chris Lattner571e4342006-10-27 21:36:01 +00001284 return;
1285 }
1286
Chris Lattnerd2f9ee92006-10-27 21:54:23 +00001287
1288 // Create TmpBB after CurBB.
Chris Lattner571e4342006-10-27 21:36:01 +00001289 MachineFunction::iterator BBI = CurBB;
1290 MachineBasicBlock *TmpBB = new MachineBasicBlock(CurBB->getBasicBlock());
1291 CurBB->getParent()->getBasicBlockList().insert(++BBI, TmpBB);
1292
Chris Lattnerd2f9ee92006-10-27 21:54:23 +00001293 if (Opc == Instruction::Or) {
1294 // Codegen X | Y as:
1295 // jmp_if_X TBB
1296 // jmp TmpBB
1297 // TmpBB:
1298 // jmp_if_Y TBB
1299 // jmp FBB
1300 //
Chris Lattner571e4342006-10-27 21:36:01 +00001301
Chris Lattnerd2f9ee92006-10-27 21:54:23 +00001302 // Emit the LHS condition.
1303 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
1304
1305 // Emit the RHS condition into TmpBB.
1306 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1307 } else {
1308 assert(Opc == Instruction::And && "Unknown merge op!");
1309 // Codegen X & Y as:
1310 // jmp_if_X TmpBB
1311 // jmp FBB
1312 // TmpBB:
1313 // jmp_if_Y TBB
1314 // jmp FBB
1315 //
1316 // This requires creation of TmpBB after CurBB.
1317
1318 // Emit the LHS condition.
1319 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
1320
1321 // Emit the RHS condition into TmpBB.
1322 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1323 }
Chris Lattner571e4342006-10-27 21:36:01 +00001324}
1325
Chris Lattnerdf19f272006-10-31 22:37:42 +00001326/// If the set of cases should be emitted as a series of branches, return true.
1327/// If we should emit this as a bunch of and/or'd together conditions, return
1328/// false.
1329static bool
1330ShouldEmitAsBranches(const std::vector<SelectionDAGISel::CaseBlock> &Cases) {
1331 if (Cases.size() != 2) return true;
1332
Chris Lattner0ccb5002006-10-31 23:06:00 +00001333 // If this is two comparisons of the same values or'd or and'd together, they
1334 // will get folded into a single comparison, so don't emit two blocks.
1335 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1336 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1337 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1338 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1339 return false;
1340 }
1341
Chris Lattnerdf19f272006-10-31 22:37:42 +00001342 return true;
1343}
1344
Chris Lattner1c08c712005-01-07 07:47:53 +00001345void SelectionDAGLowering::visitBr(BranchInst &I) {
1346 // Update machine-CFG edges.
1347 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
Chris Lattner1c08c712005-01-07 07:47:53 +00001348
1349 // Figure out which block is immediately after the current one.
1350 MachineBasicBlock *NextBlock = 0;
1351 MachineFunction::iterator BBI = CurMBB;
1352 if (++BBI != CurMBB->getParent()->end())
1353 NextBlock = BBI;
1354
1355 if (I.isUnconditional()) {
1356 // If this is not a fall-through branch, emit the branch.
1357 if (Succ0MBB != NextBlock)
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001358 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001359 DAG.getBasicBlock(Succ0MBB)));
Chris Lattner1c08c712005-01-07 07:47:53 +00001360
Chris Lattner57ab6592006-10-24 17:57:59 +00001361 // Update machine-CFG edges.
1362 CurMBB->addSuccessor(Succ0MBB);
Chris Lattner57ab6592006-10-24 17:57:59 +00001363 return;
1364 }
1365
1366 // If this condition is one of the special cases we handle, do special stuff
1367 // now.
1368 Value *CondVal = I.getCondition();
Chris Lattner57ab6592006-10-24 17:57:59 +00001369 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
Chris Lattner571e4342006-10-27 21:36:01 +00001370
1371 // If this is a series of conditions that are or'd or and'd together, emit
1372 // this as a sequence of branches instead of setcc's with and/or operations.
1373 // For example, instead of something like:
1374 // cmp A, B
1375 // C = seteq
1376 // cmp D, E
1377 // F = setle
1378 // or C, F
1379 // jnz foo
1380 // Emit:
1381 // cmp A, B
1382 // je foo
1383 // cmp D, E
1384 // jle foo
1385 //
1386 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
1387 if (BOp->hasOneUse() &&
Chris Lattnerd2f9ee92006-10-27 21:54:23 +00001388 (BOp->getOpcode() == Instruction::And ||
Chris Lattner571e4342006-10-27 21:36:01 +00001389 BOp->getOpcode() == Instruction::Or)) {
1390 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
Chris Lattner0ccb5002006-10-31 23:06:00 +00001391 // If the compares in later blocks need to use values not currently
1392 // exported from this block, export them now. This block should always
1393 // be the first entry.
1394 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
1395
Chris Lattnerdf19f272006-10-31 22:37:42 +00001396 // Allow some cases to be rejected.
1397 if (ShouldEmitAsBranches(SwitchCases)) {
Chris Lattnerdf19f272006-10-31 22:37:42 +00001398 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1399 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1400 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1401 }
1402
1403 // Emit the branch for this block.
1404 visitSwitchCase(SwitchCases[0]);
1405 SwitchCases.erase(SwitchCases.begin());
1406 return;
Chris Lattner5a145f02006-10-29 18:23:37 +00001407 }
1408
Chris Lattner0ccb5002006-10-31 23:06:00 +00001409 // Okay, we decided not to do this, remove any inserted MBB's and clear
1410 // SwitchCases.
1411 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1412 CurMBB->getParent()->getBasicBlockList().erase(SwitchCases[i].ThisBB);
1413
Chris Lattnerdf19f272006-10-31 22:37:42 +00001414 SwitchCases.clear();
Chris Lattner571e4342006-10-27 21:36:01 +00001415 }
1416 }
Chris Lattner24525952006-10-24 18:07:37 +00001417
1418 // Create a CaseBlock record representing this branch.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001419 SelectionDAGISel::CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(),
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001420 NULL, Succ0MBB, Succ1MBB, CurMBB);
Chris Lattner24525952006-10-24 18:07:37 +00001421 // Use visitSwitchCase to actually insert the fast branch sequence for this
1422 // cond branch.
1423 visitSwitchCase(CB);
Chris Lattner1c08c712005-01-07 07:47:53 +00001424}
1425
Nate Begemanf15485a2006-03-27 01:32:24 +00001426/// visitSwitchCase - Emits the necessary code to represent a single node in
1427/// the binary search tree resulting from lowering a switch instruction.
1428void SelectionDAGLowering::visitSwitchCase(SelectionDAGISel::CaseBlock &CB) {
Chris Lattner57ab6592006-10-24 17:57:59 +00001429 SDOperand Cond;
1430 SDOperand CondLHS = getValue(CB.CmpLHS);
1431
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001432 // Build the setcc now.
1433 if (CB.CmpMHS == NULL) {
1434 // Fold "(X == true)" to X and "(X == false)" to !X to
1435 // handle common cases produced by branch lowering.
1436 if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ)
1437 Cond = CondLHS;
1438 else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) {
1439 SDOperand True = DAG.getConstant(1, CondLHS.getValueType());
1440 Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True);
1441 } else
1442 Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1443 } else {
1444 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001445
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001446 uint64_t Low = cast<ConstantInt>(CB.CmpLHS)->getSExtValue();
1447 uint64_t High = cast<ConstantInt>(CB.CmpRHS)->getSExtValue();
1448
1449 SDOperand CmpOp = getValue(CB.CmpMHS);
1450 MVT::ValueType VT = CmpOp.getValueType();
1451
1452 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1453 Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE);
1454 } else {
1455 SDOperand SUB = DAG.getNode(ISD::SUB, VT, CmpOp, DAG.getConstant(Low, VT));
1456 Cond = DAG.getSetCC(MVT::i1, SUB,
1457 DAG.getConstant(High-Low, VT), ISD::SETULE);
1458 }
1459
1460 }
1461
Nate Begemanf15485a2006-03-27 01:32:24 +00001462 // Set NextBlock to be the MBB immediately after the current one, if any.
1463 // This is used to avoid emitting unnecessary branches to the next block.
1464 MachineBasicBlock *NextBlock = 0;
1465 MachineFunction::iterator BBI = CurMBB;
1466 if (++BBI != CurMBB->getParent()->end())
1467 NextBlock = BBI;
1468
1469 // If the lhs block is the next block, invert the condition so that we can
1470 // fall through to the lhs instead of the rhs block.
Chris Lattner57ab6592006-10-24 17:57:59 +00001471 if (CB.TrueBB == NextBlock) {
1472 std::swap(CB.TrueBB, CB.FalseBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001473 SDOperand True = DAG.getConstant(1, Cond.getValueType());
1474 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
1475 }
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001476 SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond,
Chris Lattner57ab6592006-10-24 17:57:59 +00001477 DAG.getBasicBlock(CB.TrueBB));
1478 if (CB.FalseBB == NextBlock)
Nate Begemanf15485a2006-03-27 01:32:24 +00001479 DAG.setRoot(BrCond);
1480 else
1481 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
Chris Lattner57ab6592006-10-24 17:57:59 +00001482 DAG.getBasicBlock(CB.FalseBB)));
Nate Begemanf15485a2006-03-27 01:32:24 +00001483 // Update successor info
Chris Lattner57ab6592006-10-24 17:57:59 +00001484 CurMBB->addSuccessor(CB.TrueBB);
1485 CurMBB->addSuccessor(CB.FalseBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001486}
1487
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001488/// visitJumpTable - Emit JumpTable node in the current MBB
Nate Begeman37efe672006-04-22 18:53:45 +00001489void SelectionDAGLowering::visitJumpTable(SelectionDAGISel::JumpTable &JT) {
Nate Begeman37efe672006-04-22 18:53:45 +00001490 // Emit the code for the jump table
Scott Michelf147a8d2007-04-24 01:24:20 +00001491 assert(JT.Reg != -1U && "Should lower JT Header first!");
Nate Begeman37efe672006-04-22 18:53:45 +00001492 MVT::ValueType PTy = TLI.getPointerTy();
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001493 SDOperand Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001494 SDOperand Table = DAG.getJumpTable(JT.JTI, PTy);
1495 DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1),
1496 Table, Index));
1497 return;
Nate Begeman37efe672006-04-22 18:53:45 +00001498}
1499
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001500/// visitJumpTableHeader - This function emits necessary code to produce index
1501/// in the JumpTable from switch case.
1502void SelectionDAGLowering::visitJumpTableHeader(SelectionDAGISel::JumpTable &JT,
1503 SelectionDAGISel::JumpTableHeader &JTH) {
1504 // Subtract the lowest switch case value from the value being switched on
1505 // and conditional branch to default mbb if the result is greater than the
1506 // difference between smallest and largest cases.
1507 SDOperand SwitchOp = getValue(JTH.SValue);
1508 MVT::ValueType VT = SwitchOp.getValueType();
1509 SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1510 DAG.getConstant(JTH.First, VT));
1511
1512 // The SDNode we just created, which holds the value being switched on
1513 // minus the the smallest case value, needs to be copied to a virtual
1514 // register so it can be used as an index into the jump table in a
1515 // subsequent basic block. This value may be smaller or larger than the
1516 // target's pointer type, and therefore require extension or truncating.
Dan Gohman7f321562007-06-25 16:23:39 +00001517 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getPointerTy()))
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001518 SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB);
1519 else
1520 SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB);
1521
1522 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001523 SDOperand CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp);
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001524 JT.Reg = JumpTableReg;
1525
1526 // Emit the range check for the jump table, and branch to the default
1527 // block for the switch statement if the value being switched on exceeds
1528 // the largest case in the switch.
Scott Michel5b8f82e2008-03-10 15:42:14 +00001529 SDOperand CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001530 DAG.getConstant(JTH.Last-JTH.First,VT),
1531 ISD::SETUGT);
1532
1533 // Set NextBlock to be the MBB immediately after the current one, if any.
1534 // This is used to avoid emitting unnecessary branches to the next block.
1535 MachineBasicBlock *NextBlock = 0;
1536 MachineFunction::iterator BBI = CurMBB;
1537 if (++BBI != CurMBB->getParent()->end())
1538 NextBlock = BBI;
1539
1540 SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP,
1541 DAG.getBasicBlock(JT.Default));
1542
1543 if (JT.MBB == NextBlock)
1544 DAG.setRoot(BrCond);
1545 else
1546 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001547 DAG.getBasicBlock(JT.MBB)));
1548
1549 return;
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001550}
1551
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001552/// visitBitTestHeader - This function emits necessary code to produce value
1553/// suitable for "bit tests"
1554void SelectionDAGLowering::visitBitTestHeader(SelectionDAGISel::BitTestBlock &B) {
1555 // Subtract the minimum value
1556 SDOperand SwitchOp = getValue(B.SValue);
1557 MVT::ValueType VT = SwitchOp.getValueType();
1558 SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1559 DAG.getConstant(B.First, VT));
1560
1561 // Check range
Scott Michel5b8f82e2008-03-10 15:42:14 +00001562 SDOperand RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001563 DAG.getConstant(B.Range, VT),
1564 ISD::SETUGT);
1565
1566 SDOperand ShiftOp;
Dan Gohman7f321562007-06-25 16:23:39 +00001567 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getShiftAmountTy()))
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001568 ShiftOp = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), SUB);
1569 else
1570 ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), SUB);
1571
1572 // Make desired shift
1573 SDOperand SwitchVal = DAG.getNode(ISD::SHL, TLI.getPointerTy(),
1574 DAG.getConstant(1, TLI.getPointerTy()),
1575 ShiftOp);
1576
1577 unsigned SwitchReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001578 SDOperand CopyTo = DAG.getCopyToReg(getControlRoot(), SwitchReg, SwitchVal);
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001579 B.Reg = SwitchReg;
1580
1581 SDOperand BrRange = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, RangeCmp,
1582 DAG.getBasicBlock(B.Default));
1583
1584 // Set NextBlock to be the MBB immediately after the current one, if any.
1585 // This is used to avoid emitting unnecessary branches to the next block.
1586 MachineBasicBlock *NextBlock = 0;
1587 MachineFunction::iterator BBI = CurMBB;
1588 if (++BBI != CurMBB->getParent()->end())
1589 NextBlock = BBI;
1590
1591 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1592 if (MBB == NextBlock)
1593 DAG.setRoot(BrRange);
1594 else
1595 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, CopyTo,
1596 DAG.getBasicBlock(MBB)));
1597
1598 CurMBB->addSuccessor(B.Default);
1599 CurMBB->addSuccessor(MBB);
1600
1601 return;
1602}
1603
1604/// visitBitTestCase - this function produces one "bit test"
1605void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1606 unsigned Reg,
1607 SelectionDAGISel::BitTestCase &B) {
1608 // Emit bit tests and jumps
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001609 SDOperand SwitchVal = DAG.getCopyFromReg(getControlRoot(), Reg, TLI.getPointerTy());
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001610
1611 SDOperand AndOp = DAG.getNode(ISD::AND, TLI.getPointerTy(),
1612 SwitchVal,
1613 DAG.getConstant(B.Mask,
1614 TLI.getPointerTy()));
Scott Michel5b8f82e2008-03-10 15:42:14 +00001615 SDOperand AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp), AndOp,
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001616 DAG.getConstant(0, TLI.getPointerTy()),
1617 ISD::SETNE);
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001618 SDOperand BrAnd = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(),
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001619 AndCmp, DAG.getBasicBlock(B.TargetBB));
1620
1621 // Set NextBlock to be the MBB immediately after the current one, if any.
1622 // This is used to avoid emitting unnecessary branches to the next block.
1623 MachineBasicBlock *NextBlock = 0;
1624 MachineFunction::iterator BBI = CurMBB;
1625 if (++BBI != CurMBB->getParent()->end())
1626 NextBlock = BBI;
1627
1628 if (NextMBB == NextBlock)
1629 DAG.setRoot(BrAnd);
1630 else
1631 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrAnd,
1632 DAG.getBasicBlock(NextMBB)));
1633
1634 CurMBB->addSuccessor(B.TargetBB);
1635 CurMBB->addSuccessor(NextMBB);
1636
1637 return;
1638}
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00001639
Jim Laskeyb180aa12007-02-21 22:53:45 +00001640void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1641 // Retrieve successors.
1642 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00001643 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
Duncan Sands9fac0b52007-06-06 10:05:18 +00001644
Duncan Sandsfd7b3262007-12-17 18:08:19 +00001645 if (isa<InlineAsm>(I.getCalledValue()))
1646 visitInlineAsm(&I);
1647 else
Duncan Sands6f74b482007-12-19 09:48:52 +00001648 LowerCallTo(&I, getValue(I.getOperand(0)), false, LandingPad);
Duncan Sands9fac0b52007-06-06 10:05:18 +00001649
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00001650 // If the value of the invoke is used outside of its defining block, make it
1651 // available as a virtual register.
1652 if (!I.use_empty()) {
1653 DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I);
1654 if (VMI != FuncInfo.ValueMap.end())
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001655 CopyValueToVirtualRegister(&I, VMI->second);
Jim Laskey183f47f2007-02-25 21:43:59 +00001656 }
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00001657
1658 // Drop into normal successor.
Dan Gohman86e1ebf2008-03-27 19:56:19 +00001659 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00001660 DAG.getBasicBlock(Return)));
1661
1662 // Update successor info
1663 CurMBB->addSuccessor(Return);
1664 CurMBB->addSuccessor(LandingPad);
Jim Laskeyb180aa12007-02-21 22:53:45 +00001665}
1666
1667void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1668}
1669
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001670/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001671/// small case ranges).
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001672bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001673 CaseRecVector& WorkList,
1674 Value* SV,
1675 MachineBasicBlock* Default) {
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001676 Case& BackCase = *(CR.Range.second-1);
1677
1678 // Size is the number of Cases represented by this range.
1679 unsigned Size = CR.Range.second - CR.Range.first;
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001680 if (Size > 3)
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001681 return false;
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001682
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001683 // Get the MachineFunction which holds the current MBB. This is used when
1684 // inserting any additional MBBs necessary to represent the switch.
1685 MachineFunction *CurMF = CurMBB->getParent();
1686
1687 // Figure out which block is immediately after the current one.
1688 MachineBasicBlock *NextBlock = 0;
1689 MachineFunction::iterator BBI = CR.CaseBB;
1690
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001691 if (++BBI != CurMBB->getParent()->end())
1692 NextBlock = BBI;
1693
1694 // TODO: If any two of the cases has the same destination, and if one value
1695 // is the same as the other, but has one bit unset that the other has set,
1696 // use bit manipulation to do two compares at once. For example:
1697 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
1698
1699 // Rearrange the case blocks so that the last one falls through if possible.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001700 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001701 // The last case block won't fall through into 'NextBlock' if we emit the
1702 // branches in this order. See if rearranging a case value would help.
1703 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001704 if (I->BB == NextBlock) {
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001705 std::swap(*I, BackCase);
1706 break;
1707 }
1708 }
1709 }
1710
1711 // Create a CaseBlock record representing a conditional branch to
1712 // the Case's target mbb if the value being switched on SV is equal
1713 // to C.
1714 MachineBasicBlock *CurBlock = CR.CaseBB;
1715 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1716 MachineBasicBlock *FallThrough;
1717 if (I != E-1) {
1718 FallThrough = new MachineBasicBlock(CurBlock->getBasicBlock());
1719 CurMF->getBasicBlockList().insert(BBI, FallThrough);
1720 } else {
1721 // If the last case doesn't match, go to the default block.
1722 FallThrough = Default;
1723 }
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001724
1725 Value *RHS, *LHS, *MHS;
1726 ISD::CondCode CC;
1727 if (I->High == I->Low) {
1728 // This is just small small case range :) containing exactly 1 case
1729 CC = ISD::SETEQ;
1730 LHS = SV; RHS = I->High; MHS = NULL;
1731 } else {
1732 CC = ISD::SETLE;
1733 LHS = I->Low; MHS = SV; RHS = I->High;
1734 }
1735 SelectionDAGISel::CaseBlock CB(CC, LHS, RHS, MHS,
1736 I->BB, FallThrough, CurBlock);
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001737
1738 // If emitting the first comparison, just call visitSwitchCase to emit the
1739 // code into the current block. Otherwise, push the CaseBlock onto the
1740 // vector to be later processed by SDISel, and insert the node's MBB
1741 // before the next MBB.
1742 if (CurBlock == CurMBB)
1743 visitSwitchCase(CB);
1744 else
1745 SwitchCases.push_back(CB);
1746
1747 CurBlock = FallThrough;
1748 }
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001749
1750 return true;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001751}
1752
Anton Korobeynikov7294b582007-05-09 20:07:08 +00001753static inline bool areJTsAllowed(const TargetLowering &TLI) {
1754 return (TLI.isOperationLegal(ISD::BR_JT, MVT::Other) ||
1755 TLI.isOperationLegal(ISD::BRIND, MVT::Other));
1756}
1757
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001758/// handleJTSwitchCase - Emit jumptable for current switch case range
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001759bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001760 CaseRecVector& WorkList,
1761 Value* SV,
1762 MachineBasicBlock* Default) {
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001763 Case& FrontCase = *CR.Range.first;
1764 Case& BackCase = *(CR.Range.second-1);
1765
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001766 int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1767 int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue();
1768
1769 uint64_t TSize = 0;
1770 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1771 I!=E; ++I)
1772 TSize += I->size();
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001773
Anton Korobeynikov7294b582007-05-09 20:07:08 +00001774 if (!areJTsAllowed(TLI) || TSize <= 3)
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001775 return false;
1776
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001777 double Density = (double)TSize / (double)((Last - First) + 1ULL);
1778 if (Density < 0.4)
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001779 return false;
1780
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001781 DOUT << "Lowering jump table\n"
1782 << "First entry: " << First << ". Last entry: " << Last << "\n"
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001783 << "Size: " << TSize << ". Density: " << Density << "\n\n";
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001784
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001785 // Get the MachineFunction which holds the current MBB. This is used when
1786 // inserting any additional MBBs necessary to represent the switch.
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001787 MachineFunction *CurMF = CurMBB->getParent();
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001788
1789 // Figure out which block is immediately after the current one.
1790 MachineBasicBlock *NextBlock = 0;
1791 MachineFunction::iterator BBI = CR.CaseBB;
1792
1793 if (++BBI != CurMBB->getParent()->end())
1794 NextBlock = BBI;
1795
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001796 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1797
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001798 // Create a new basic block to hold the code for loading the address
1799 // of the jump table, and jumping to it. Update successor information;
1800 // we will either branch to the default case for the switch, or the jump
1801 // table.
1802 MachineBasicBlock *JumpTableBB = new MachineBasicBlock(LLVMBB);
1803 CurMF->getBasicBlockList().insert(BBI, JumpTableBB);
1804 CR.CaseBB->addSuccessor(Default);
1805 CR.CaseBB->addSuccessor(JumpTableBB);
1806
1807 // Build a vector of destination BBs, corresponding to each target
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001808 // of the jump table. If the value of the jump table slot corresponds to
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001809 // a case statement, push the case's BB onto the vector, otherwise, push
1810 // the default BB.
1811 std::vector<MachineBasicBlock*> DestBBs;
1812 int64_t TEI = First;
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001813 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
1814 int64_t Low = cast<ConstantInt>(I->Low)->getSExtValue();
1815 int64_t High = cast<ConstantInt>(I->High)->getSExtValue();
1816
1817 if ((Low <= TEI) && (TEI <= High)) {
1818 DestBBs.push_back(I->BB);
1819 if (TEI==High)
1820 ++I;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001821 } else {
1822 DestBBs.push_back(Default);
1823 }
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001824 }
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001825
1826 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001827 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001828 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
1829 E = DestBBs.end(); I != E; ++I) {
1830 if (!SuccsHandled[(*I)->getNumber()]) {
1831 SuccsHandled[(*I)->getNumber()] = true;
1832 JumpTableBB->addSuccessor(*I);
1833 }
1834 }
1835
1836 // Create a jump table index for this jump table, or return an existing
1837 // one.
1838 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
1839
1840 // Set the jump table information so that we can codegen it as a second
1841 // MachineBasicBlock
Scott Michelf147a8d2007-04-24 01:24:20 +00001842 SelectionDAGISel::JumpTable JT(-1U, JTI, JumpTableBB, Default);
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001843 SelectionDAGISel::JumpTableHeader JTH(First, Last, SV, CR.CaseBB,
1844 (CR.CaseBB == CurMBB));
1845 if (CR.CaseBB == CurMBB)
1846 visitJumpTableHeader(JT, JTH);
1847
1848 JTCases.push_back(SelectionDAGISel::JumpTableBlock(JTH, JT));
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001849
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001850 return true;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001851}
1852
1853/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1854/// 2 subtrees.
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001855bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001856 CaseRecVector& WorkList,
1857 Value* SV,
1858 MachineBasicBlock* Default) {
1859 // Get the MachineFunction which holds the current MBB. This is used when
1860 // inserting any additional MBBs necessary to represent the switch.
1861 MachineFunction *CurMF = CurMBB->getParent();
1862
1863 // Figure out which block is immediately after the current one.
1864 MachineBasicBlock *NextBlock = 0;
1865 MachineFunction::iterator BBI = CR.CaseBB;
1866
1867 if (++BBI != CurMBB->getParent()->end())
1868 NextBlock = BBI;
1869
1870 Case& FrontCase = *CR.Range.first;
1871 Case& BackCase = *(CR.Range.second-1);
1872 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1873
1874 // Size is the number of Cases represented by this range.
1875 unsigned Size = CR.Range.second - CR.Range.first;
1876
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001877 int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1878 int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue();
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001879 double FMetric = 0;
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001880 CaseItr Pivot = CR.Range.first + Size/2;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001881
1882 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1883 // (heuristically) allow us to emit JumpTable's later.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001884 uint64_t TSize = 0;
1885 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1886 I!=E; ++I)
1887 TSize += I->size();
1888
1889 uint64_t LSize = FrontCase.size();
1890 uint64_t RSize = TSize-LSize;
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001891 DOUT << "Selecting best pivot: \n"
1892 << "First: " << First << ", Last: " << Last <<"\n"
1893 << "LSize: " << LSize << ", RSize: " << RSize << "\n";
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001894 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001895 J!=E; ++I, ++J) {
1896 int64_t LEnd = cast<ConstantInt>(I->High)->getSExtValue();
1897 int64_t RBegin = cast<ConstantInt>(J->Low)->getSExtValue();
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001898 assert((RBegin-LEnd>=1) && "Invalid case distance");
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001899 double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL);
1900 double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL);
Anton Korobeynikov54e2b142007-04-09 21:57:03 +00001901 double Metric = Log2_64(RBegin-LEnd)*(LDensity+RDensity);
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001902 // Should always split in some non-trivial place
1903 DOUT <<"=>Step\n"
1904 << "LEnd: " << LEnd << ", RBegin: " << RBegin << "\n"
1905 << "LDensity: " << LDensity << ", RDensity: " << RDensity << "\n"
1906 << "Metric: " << Metric << "\n";
1907 if (FMetric < Metric) {
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001908 Pivot = J;
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001909 FMetric = Metric;
1910 DOUT << "Current metric set to: " << FMetric << "\n";
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001911 }
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001912
1913 LSize += J->size();
1914 RSize -= J->size();
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001915 }
Anton Korobeynikov7294b582007-05-09 20:07:08 +00001916 if (areJTsAllowed(TLI)) {
1917 // If our case is dense we *really* should handle it earlier!
1918 assert((FMetric > 0) && "Should handle dense range earlier!");
1919 } else {
1920 Pivot = CR.Range.first + Size/2;
1921 }
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001922
1923 CaseRange LHSR(CR.Range.first, Pivot);
1924 CaseRange RHSR(Pivot, CR.Range.second);
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001925 Constant *C = Pivot->Low;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001926 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
1927
1928 // We know that we branch to the LHS if the Value being switched on is
1929 // less than the Pivot value, C. We use this to optimize our binary
1930 // tree a bit, by recognizing that if SV is greater than or equal to the
1931 // LHS's Case Value, and that Case Value is exactly one less than the
1932 // Pivot's Value, then we can branch directly to the LHS's Target,
1933 // rather than creating a leaf node for it.
1934 if ((LHSR.second - LHSR.first) == 1 &&
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001935 LHSR.first->High == CR.GE &&
1936 cast<ConstantInt>(C)->getSExtValue() ==
1937 (cast<ConstantInt>(CR.GE)->getSExtValue() + 1LL)) {
1938 TrueBB = LHSR.first->BB;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001939 } else {
1940 TrueBB = new MachineBasicBlock(LLVMBB);
1941 CurMF->getBasicBlockList().insert(BBI, TrueBB);
1942 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
1943 }
1944
1945 // Similar to the optimization above, if the Value being switched on is
1946 // known to be less than the Constant CR.LT, and the current Case Value
1947 // is CR.LT - 1, then we can branch directly to the target block for
1948 // the current Case Value, rather than emitting a RHS leaf node for it.
1949 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001950 cast<ConstantInt>(RHSR.first->Low)->getSExtValue() ==
1951 (cast<ConstantInt>(CR.LT)->getSExtValue() - 1LL)) {
1952 FalseBB = RHSR.first->BB;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001953 } else {
1954 FalseBB = new MachineBasicBlock(LLVMBB);
1955 CurMF->getBasicBlockList().insert(BBI, FalseBB);
1956 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
1957 }
1958
1959 // Create a CaseBlock record representing a conditional branch to
1960 // the LHS node if the value being switched on SV is less than C.
1961 // Otherwise, branch to LHS.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00001962 SelectionDAGISel::CaseBlock CB(ISD::SETLT, SV, C, NULL,
1963 TrueBB, FalseBB, CR.CaseBB);
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001964
1965 if (CR.CaseBB == CurMBB)
1966 visitSwitchCase(CB);
1967 else
1968 SwitchCases.push_back(CB);
Anton Korobeynikovdd433212007-03-27 12:05:48 +00001969
1970 return true;
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00001971}
1972
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001973/// handleBitTestsSwitchCase - if current case range has few destination and
1974/// range span less, than machine word bitwidth, encode case range into series
1975/// of masks and emit bit tests with these masks.
1976bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1977 CaseRecVector& WorkList,
1978 Value* SV,
Chris Lattner3ff98172007-04-14 02:26:56 +00001979 MachineBasicBlock* Default){
Dan Gohmanb55757e2007-05-18 17:52:13 +00001980 unsigned IntPtrBits = MVT::getSizeInBits(TLI.getPointerTy());
Anton Korobeynikov4198c582007-04-09 12:31:58 +00001981
1982 Case& FrontCase = *CR.Range.first;
1983 Case& BackCase = *(CR.Range.second-1);
1984
1985 // Get the MachineFunction which holds the current MBB. This is used when
1986 // inserting any additional MBBs necessary to represent the switch.
1987 MachineFunction *CurMF = CurMBB->getParent();
1988
1989 unsigned numCmps = 0;
1990 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1991 I!=E; ++I) {
1992 // Single case counts one, case range - two.
1993 if (I->Low == I->High)
1994 numCmps +=1;
1995 else
1996 numCmps +=2;
1997 }
1998
1999 // Count unique destinations
2000 SmallSet<MachineBasicBlock*, 4> Dests;
2001 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2002 Dests.insert(I->BB);
2003 if (Dests.size() > 3)
2004 // Don't bother the code below, if there are too much unique destinations
2005 return false;
2006 }
2007 DOUT << "Total number of unique destinations: " << Dests.size() << "\n"
2008 << "Total number of comparisons: " << numCmps << "\n";
2009
2010 // Compute span of values.
2011 Constant* minValue = FrontCase.Low;
2012 Constant* maxValue = BackCase.High;
2013 uint64_t range = cast<ConstantInt>(maxValue)->getSExtValue() -
2014 cast<ConstantInt>(minValue)->getSExtValue();
2015 DOUT << "Compare range: " << range << "\n"
2016 << "Low bound: " << cast<ConstantInt>(minValue)->getSExtValue() << "\n"
2017 << "High bound: " << cast<ConstantInt>(maxValue)->getSExtValue() << "\n";
2018
Anton Korobeynikovab8fd402007-04-26 20:44:04 +00002019 if (range>=IntPtrBits ||
Anton Korobeynikov4198c582007-04-09 12:31:58 +00002020 (!(Dests.size() == 1 && numCmps >= 3) &&
2021 !(Dests.size() == 2 && numCmps >= 5) &&
2022 !(Dests.size() >= 3 && numCmps >= 6)))
2023 return false;
2024
2025 DOUT << "Emitting bit tests\n";
2026 int64_t lowBound = 0;
2027
2028 // Optimize the case where all the case values fit in a
2029 // word without having to subtract minValue. In this case,
2030 // we can optimize away the subtraction.
2031 if (cast<ConstantInt>(minValue)->getSExtValue() >= 0 &&
Anton Korobeynikove01017b2007-04-14 13:25:55 +00002032 cast<ConstantInt>(maxValue)->getSExtValue() < IntPtrBits) {
Anton Korobeynikov4198c582007-04-09 12:31:58 +00002033 range = cast<ConstantInt>(maxValue)->getSExtValue();
2034 } else {
2035 lowBound = cast<ConstantInt>(minValue)->getSExtValue();
2036 }
2037
2038 CaseBitsVector CasesBits;
2039 unsigned i, count = 0;
2040
2041 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2042 MachineBasicBlock* Dest = I->BB;
2043 for (i = 0; i < count; ++i)
2044 if (Dest == CasesBits[i].BB)
2045 break;
2046
2047 if (i == count) {
2048 assert((count < 3) && "Too much destinations to test!");
2049 CasesBits.push_back(CaseBits(0, Dest, 0));
2050 count++;
2051 }
2052
2053 uint64_t lo = cast<ConstantInt>(I->Low)->getSExtValue() - lowBound;
2054 uint64_t hi = cast<ConstantInt>(I->High)->getSExtValue() - lowBound;
2055
2056 for (uint64_t j = lo; j <= hi; j++) {
Anton Korobeynikove01017b2007-04-14 13:25:55 +00002057 CasesBits[i].Mask |= 1ULL << j;
Anton Korobeynikov4198c582007-04-09 12:31:58 +00002058 CasesBits[i].Bits++;
2059 }
2060
2061 }
2062 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
2063
2064 SelectionDAGISel::BitTestInfo BTC;
2065
2066 // Figure out which block is immediately after the current one.
2067 MachineFunction::iterator BBI = CR.CaseBB;
2068 ++BBI;
2069
2070 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2071
2072 DOUT << "Cases:\n";
2073 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
2074 DOUT << "Mask: " << CasesBits[i].Mask << ", Bits: " << CasesBits[i].Bits
2075 << ", BB: " << CasesBits[i].BB << "\n";
2076
2077 MachineBasicBlock *CaseBB = new MachineBasicBlock(LLVMBB);
2078 CurMF->getBasicBlockList().insert(BBI, CaseBB);
2079 BTC.push_back(SelectionDAGISel::BitTestCase(CasesBits[i].Mask,
2080 CaseBB,
2081 CasesBits[i].BB));
2082 }
2083
2084 SelectionDAGISel::BitTestBlock BTB(lowBound, range, SV,
Jeff Cohenefc36622007-04-09 14:32:59 +00002085 -1U, (CR.CaseBB == CurMBB),
Anton Korobeynikov4198c582007-04-09 12:31:58 +00002086 CR.CaseBB, Default, BTC);
2087
2088 if (CR.CaseBB == CurMBB)
2089 visitBitTestHeader(BTB);
2090
2091 BitTestCases.push_back(BTB);
2092
2093 return true;
2094}
2095
2096
Dan Gohman86e1ebf2008-03-27 19:56:19 +00002097/// Clusterify - Transform simple list of Cases into list of CaseRange's
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002098unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases,
2099 const SwitchInst& SI) {
2100 unsigned numCmps = 0;
2101
2102 // Start with "simple" cases
2103 for (unsigned i = 1; i < SI.getNumSuccessors(); ++i) {
2104 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2105 Cases.push_back(Case(SI.getSuccessorValue(i),
2106 SI.getSuccessorValue(i),
2107 SMBB));
2108 }
Chris Lattnerb3d9cdb2007-11-27 06:14:32 +00002109 std::sort(Cases.begin(), Cases.end(), CaseCmp());
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002110
2111 // Merge case into clusters
2112 if (Cases.size()>=2)
David Greenea2a48852007-06-29 03:42:23 +00002113 // Must recompute end() each iteration because it may be
2114 // invalidated by erase if we hold on to it
Chris Lattner27a6c732007-11-24 07:07:01 +00002115 for (CaseItr I=Cases.begin(), J=++(Cases.begin()); J!=Cases.end(); ) {
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002116 int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
2117 int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
2118 MachineBasicBlock* nextBB = J->BB;
2119 MachineBasicBlock* currentBB = I->BB;
2120
2121 // If the two neighboring cases go to the same destination, merge them
2122 // into a single case.
2123 if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
2124 I->High = J->High;
2125 J = Cases.erase(J);
2126 } else {
2127 I = J++;
2128 }
2129 }
2130
2131 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2132 if (I->Low != I->High)
2133 // A range counts double, since it requires two compares.
2134 ++numCmps;
2135 }
2136
2137 return numCmps;
2138}
2139
2140void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
Nate Begemanf15485a2006-03-27 01:32:24 +00002141 // Figure out which block is immediately after the current one.
2142 MachineBasicBlock *NextBlock = 0;
2143 MachineFunction::iterator BBI = CurMBB;
Bill Wendlingc70ddad2006-10-19 21:46:38 +00002144
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002145 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
Chris Lattnerd2c1d222006-10-22 21:36:53 +00002146
Nate Begemanf15485a2006-03-27 01:32:24 +00002147 // If there is only the default destination, branch to it if it is not the
2148 // next basic block. Otherwise, just fall through.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002149 if (SI.getNumOperands() == 2) {
Nate Begemanf15485a2006-03-27 01:32:24 +00002150 // Update machine-CFG edges.
Bill Wendlingc70ddad2006-10-19 21:46:38 +00002151
Nate Begemanf15485a2006-03-27 01:32:24 +00002152 // If this is not a fall-through branch, emit the branch.
Chris Lattnerd2c1d222006-10-22 21:36:53 +00002153 if (Default != NextBlock)
Dan Gohman86e1ebf2008-03-27 19:56:19 +00002154 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
Chris Lattnerd2c1d222006-10-22 21:36:53 +00002155 DAG.getBasicBlock(Default)));
Bill Wendlingc70ddad2006-10-19 21:46:38 +00002156
Chris Lattnerd2c1d222006-10-22 21:36:53 +00002157 CurMBB->addSuccessor(Default);
Nate Begemanf15485a2006-03-27 01:32:24 +00002158 return;
2159 }
2160
2161 // If there are any non-default case statements, create a vector of Cases
2162 // representing each one, and sort the vector so that we can efficiently
2163 // create a binary search tree from them.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002164 CaseVector Cases;
2165 unsigned numCmps = Clusterify(Cases, SI);
2166 DOUT << "Clusterify finished. Total clusters: " << Cases.size()
2167 << ". Total compares: " << numCmps << "\n";
Bill Wendlingc70ddad2006-10-19 21:46:38 +00002168
Nate Begemanf15485a2006-03-27 01:32:24 +00002169 // Get the Value to be switched on and default basic blocks, which will be
2170 // inserted into CaseBlock records, representing basic blocks in the binary
2171 // search tree.
Anton Korobeynikov5502bf62007-04-04 21:14:49 +00002172 Value *SV = SI.getOperand(0);
Nate Begeman37efe672006-04-22 18:53:45 +00002173
Nate Begemanf15485a2006-03-27 01:32:24 +00002174 // Push the initial CaseRec onto the worklist
Anton Korobeynikovb17b08d2007-03-27 11:29:11 +00002175 CaseRecVector WorkList;
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00002176 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2177
2178 while (!WorkList.empty()) {
Nate Begemanf15485a2006-03-27 01:32:24 +00002179 // Grab a record representing a case range to process off the worklist
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00002180 CaseRec CR = WorkList.back();
2181 WorkList.pop_back();
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00002182
Anton Korobeynikov4198c582007-04-09 12:31:58 +00002183 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2184 continue;
2185
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00002186 // If the range has few cases (two or less) emit a series of specific
2187 // tests.
Anton Korobeynikovdd433212007-03-27 12:05:48 +00002188 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2189 continue;
2190
Anton Korobeynikov4198c582007-04-09 12:31:58 +00002191 // If the switch has more than 5 blocks, and at least 40% dense, and the
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00002192 // target supports indirect branches, then emit a jump table rather than
2193 // lowering the switch to a binary tree of conditional branches.
Anton Korobeynikovdd433212007-03-27 12:05:48 +00002194 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2195 continue;
2196
2197 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2198 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2199 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
Nate Begemanf15485a2006-03-27 01:32:24 +00002200 }
2201}
2202
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00002203
Chris Lattnerb9fccc42005-04-02 05:04:50 +00002204void SelectionDAGLowering::visitSub(User &I) {
2205 // -0.0 - X --> fneg
Reid Spencer24d6da52007-01-21 00:29:26 +00002206 const Type *Ty = I.getType();
Reid Spencer9d6565a2007-02-15 02:26:10 +00002207 if (isa<VectorType>(Ty)) {
Dan Gohman7f321562007-06-25 16:23:39 +00002208 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2209 const VectorType *DestTy = cast<VectorType>(I.getType());
2210 const Type *ElTy = DestTy->getElementType();
Evan Chengc45453f2007-06-29 21:44:35 +00002211 if (ElTy->isFloatingPoint()) {
2212 unsigned VL = DestTy->getNumElements();
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002213 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Evan Chengc45453f2007-06-29 21:44:35 +00002214 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
2215 if (CV == CNZ) {
2216 SDOperand Op2 = getValue(I.getOperand(1));
2217 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2218 return;
2219 }
Dan Gohman7f321562007-06-25 16:23:39 +00002220 }
2221 }
2222 }
2223 if (Ty->isFloatingPoint()) {
Chris Lattner01b3d732005-09-28 22:28:18 +00002224 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002225 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Chris Lattner01b3d732005-09-28 22:28:18 +00002226 SDOperand Op2 = getValue(I.getOperand(1));
2227 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2228 return;
2229 }
Dan Gohman7f321562007-06-25 16:23:39 +00002230 }
2231
2232 visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB);
Chris Lattnerb9fccc42005-04-02 05:04:50 +00002233}
2234
Dan Gohman7f321562007-06-25 16:23:39 +00002235void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
Chris Lattner1c08c712005-01-07 07:47:53 +00002236 SDOperand Op1 = getValue(I.getOperand(0));
2237 SDOperand Op2 = getValue(I.getOperand(1));
Reid Spencer24d6da52007-01-21 00:29:26 +00002238
2239 setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2));
Reid Spencer1628cec2006-10-26 06:15:43 +00002240}
2241
Nate Begemane21ea612005-11-18 07:42:56 +00002242void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2243 SDOperand Op1 = getValue(I.getOperand(0));
2244 SDOperand Op2 = getValue(I.getOperand(1));
2245
Dan Gohman7f321562007-06-25 16:23:39 +00002246 if (MVT::getSizeInBits(TLI.getShiftAmountTy()) <
2247 MVT::getSizeInBits(Op2.getValueType()))
Reid Spencer832254e2007-02-02 02:16:23 +00002248 Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2);
2249 else if (TLI.getShiftAmountTy() > Op2.getValueType())
2250 Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
Nate Begemane21ea612005-11-18 07:42:56 +00002251
Chris Lattner1c08c712005-01-07 07:47:53 +00002252 setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
2253}
2254
Reid Spencer45fb3f32006-11-20 01:22:35 +00002255void SelectionDAGLowering::visitICmp(User &I) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00002256 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2257 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2258 predicate = IC->getPredicate();
2259 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2260 predicate = ICmpInst::Predicate(IC->getPredicate());
2261 SDOperand Op1 = getValue(I.getOperand(0));
2262 SDOperand Op2 = getValue(I.getOperand(1));
Reid Spencer45fb3f32006-11-20 01:22:35 +00002263 ISD::CondCode Opcode;
Reid Spencere4d87aa2006-12-23 06:05:41 +00002264 switch (predicate) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00002265 case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break;
2266 case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break;
2267 case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break;
2268 case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break;
2269 case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break;
2270 case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break;
2271 case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break;
2272 case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break;
2273 case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break;
2274 case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break;
2275 default:
2276 assert(!"Invalid ICmp predicate value");
2277 Opcode = ISD::SETEQ;
2278 break;
2279 }
2280 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
2281}
2282
2283void SelectionDAGLowering::visitFCmp(User &I) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00002284 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2285 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2286 predicate = FC->getPredicate();
2287 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2288 predicate = FCmpInst::Predicate(FC->getPredicate());
Chris Lattner1c08c712005-01-07 07:47:53 +00002289 SDOperand Op1 = getValue(I.getOperand(0));
2290 SDOperand Op2 = getValue(I.getOperand(1));
Reid Spencere4d87aa2006-12-23 06:05:41 +00002291 ISD::CondCode Condition, FOC, FPC;
2292 switch (predicate) {
2293 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
2294 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
2295 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
2296 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
2297 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
2298 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
2299 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
2300 case FCmpInst::FCMP_ORD: FOC = ISD::SETEQ; FPC = ISD::SETO; break;
2301 case FCmpInst::FCMP_UNO: FOC = ISD::SETNE; FPC = ISD::SETUO; break;
2302 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
2303 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
2304 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
2305 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
2306 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
2307 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
2308 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
2309 default:
2310 assert(!"Invalid FCmp predicate value");
2311 FOC = FPC = ISD::SETFALSE;
2312 break;
2313 }
2314 if (FiniteOnlyFPMath())
2315 Condition = FOC;
2316 else
2317 Condition = FPC;
2318 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition));
Chris Lattner1c08c712005-01-07 07:47:53 +00002319}
2320
2321void SelectionDAGLowering::visitSelect(User &I) {
2322 SDOperand Cond = getValue(I.getOperand(0));
2323 SDOperand TrueVal = getValue(I.getOperand(1));
2324 SDOperand FalseVal = getValue(I.getOperand(2));
Dan Gohman7f321562007-06-25 16:23:39 +00002325 setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
2326 TrueVal, FalseVal));
Chris Lattner1c08c712005-01-07 07:47:53 +00002327}
2328
Reid Spencer3da59db2006-11-27 01:05:10 +00002329
2330void SelectionDAGLowering::visitTrunc(User &I) {
2331 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2332 SDOperand N = getValue(I.getOperand(0));
2333 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2334 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2335}
2336
2337void SelectionDAGLowering::visitZExt(User &I) {
2338 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2339 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2340 SDOperand N = getValue(I.getOperand(0));
2341 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2342 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2343}
2344
2345void SelectionDAGLowering::visitSExt(User &I) {
2346 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2347 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2348 SDOperand N = getValue(I.getOperand(0));
2349 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2350 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
2351}
2352
2353void SelectionDAGLowering::visitFPTrunc(User &I) {
2354 // FPTrunc is never a no-op cast, no need to check
2355 SDOperand N = getValue(I.getOperand(0));
2356 MVT::ValueType DestVT = TLI.getValueType(I.getType());
Chris Lattner0bd48932008-01-17 07:00:52 +00002357 setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N, DAG.getIntPtrConstant(0)));
Reid Spencer3da59db2006-11-27 01:05:10 +00002358}
2359
2360void SelectionDAGLowering::visitFPExt(User &I){
2361 // FPTrunc is never a no-op cast, no need to check
2362 SDOperand N = getValue(I.getOperand(0));
2363 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2364 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
2365}
2366
2367void SelectionDAGLowering::visitFPToUI(User &I) {
2368 // FPToUI is never a no-op cast, no need to check
2369 SDOperand N = getValue(I.getOperand(0));
2370 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2371 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
2372}
2373
2374void SelectionDAGLowering::visitFPToSI(User &I) {
2375 // FPToSI is never a no-op cast, no need to check
2376 SDOperand N = getValue(I.getOperand(0));
2377 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2378 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
2379}
2380
2381void SelectionDAGLowering::visitUIToFP(User &I) {
2382 // UIToFP is never a no-op cast, no need to check
2383 SDOperand N = getValue(I.getOperand(0));
2384 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2385 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
2386}
2387
2388void SelectionDAGLowering::visitSIToFP(User &I){
2389 // UIToFP is never a no-op cast, no need to check
2390 SDOperand N = getValue(I.getOperand(0));
2391 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2392 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
2393}
2394
2395void SelectionDAGLowering::visitPtrToInt(User &I) {
2396 // What to do depends on the size of the integer and the size of the pointer.
2397 // We can either truncate, zero extend, or no-op, accordingly.
Chris Lattner1c08c712005-01-07 07:47:53 +00002398 SDOperand N = getValue(I.getOperand(0));
Chris Lattnere25ca692006-03-22 20:09:35 +00002399 MVT::ValueType SrcVT = N.getValueType();
Chris Lattner28b5b1c2006-03-15 22:19:46 +00002400 MVT::ValueType DestVT = TLI.getValueType(I.getType());
Reid Spencer3da59db2006-11-27 01:05:10 +00002401 SDOperand Result;
2402 if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT))
2403 Result = DAG.getNode(ISD::TRUNCATE, DestVT, N);
2404 else
2405 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2406 Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N);
2407 setValue(&I, Result);
2408}
Chris Lattner1c08c712005-01-07 07:47:53 +00002409
Reid Spencer3da59db2006-11-27 01:05:10 +00002410void SelectionDAGLowering::visitIntToPtr(User &I) {
2411 // What to do depends on the size of the integer and the size of the pointer.
2412 // We can either truncate, zero extend, or no-op, accordingly.
2413 SDOperand N = getValue(I.getOperand(0));
2414 MVT::ValueType SrcVT = N.getValueType();
2415 MVT::ValueType DestVT = TLI.getValueType(I.getType());
2416 if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT))
2417 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2418 else
2419 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2420 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2421}
2422
2423void SelectionDAGLowering::visitBitCast(User &I) {
2424 SDOperand N = getValue(I.getOperand(0));
2425 MVT::ValueType DestVT = TLI.getValueType(I.getType());
Reid Spencer3da59db2006-11-27 01:05:10 +00002426
2427 // BitCast assures us that source and destination are the same size so this
2428 // is either a BIT_CONVERT or a no-op.
2429 if (DestVT != N.getValueType())
2430 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types
2431 else
2432 setValue(&I, N); // noop cast.
Chris Lattner1c08c712005-01-07 07:47:53 +00002433}
2434
Chris Lattner2bbd8102006-03-29 00:11:43 +00002435void SelectionDAGLowering::visitInsertElement(User &I) {
Chris Lattnerc7029802006-03-18 01:44:44 +00002436 SDOperand InVec = getValue(I.getOperand(0));
2437 SDOperand InVal = getValue(I.getOperand(1));
2438 SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2439 getValue(I.getOperand(2)));
2440
Dan Gohman7f321562007-06-25 16:23:39 +00002441 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT,
2442 TLI.getValueType(I.getType()),
2443 InVec, InVal, InIdx));
Chris Lattnerc7029802006-03-18 01:44:44 +00002444}
2445
Chris Lattner2bbd8102006-03-29 00:11:43 +00002446void SelectionDAGLowering::visitExtractElement(User &I) {
Chris Lattner384504c2006-03-21 20:44:12 +00002447 SDOperand InVec = getValue(I.getOperand(0));
2448 SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2449 getValue(I.getOperand(1)));
Dan Gohman7f321562007-06-25 16:23:39 +00002450 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Chris Lattner384504c2006-03-21 20:44:12 +00002451 TLI.getValueType(I.getType()), InVec, InIdx));
2452}
Chris Lattnerc7029802006-03-18 01:44:44 +00002453
Chris Lattner3e104b12006-04-08 04:15:24 +00002454void SelectionDAGLowering::visitShuffleVector(User &I) {
2455 SDOperand V1 = getValue(I.getOperand(0));
2456 SDOperand V2 = getValue(I.getOperand(1));
2457 SDOperand Mask = getValue(I.getOperand(2));
2458
Dan Gohman7f321562007-06-25 16:23:39 +00002459 setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE,
2460 TLI.getValueType(I.getType()),
2461 V1, V2, Mask));
Chris Lattner3e104b12006-04-08 04:15:24 +00002462}
2463
2464
Chris Lattner1c08c712005-01-07 07:47:53 +00002465void SelectionDAGLowering::visitGetElementPtr(User &I) {
2466 SDOperand N = getValue(I.getOperand(0));
2467 const Type *Ty = I.getOperand(0)->getType();
Chris Lattner1c08c712005-01-07 07:47:53 +00002468
2469 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2470 OI != E; ++OI) {
2471 Value *Idx = *OI;
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002472 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002473 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
Chris Lattner1c08c712005-01-07 07:47:53 +00002474 if (Field) {
2475 // N = N + Offset
Chris Lattnerb1919e22007-02-10 19:55:17 +00002476 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Chris Lattner1c08c712005-01-07 07:47:53 +00002477 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
Chris Lattner0bd48932008-01-17 07:00:52 +00002478 DAG.getIntPtrConstant(Offset));
Chris Lattner1c08c712005-01-07 07:47:53 +00002479 }
2480 Ty = StTy->getElementType(Field);
2481 } else {
2482 Ty = cast<SequentialType>(Ty)->getElementType();
Chris Lattner7cc47772005-01-07 21:56:57 +00002483
Chris Lattner7c0104b2005-11-09 04:45:33 +00002484 // If this is a constant subscript, handle it quickly.
2485 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002486 if (CI->getZExtValue() == 0) continue;
Reid Spencer47857812006-12-31 05:55:36 +00002487 uint64_t Offs =
Dale Johannesena7ac2bd2007-10-01 23:08:35 +00002488 TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00002489 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2490 DAG.getIntPtrConstant(Offs));
Chris Lattner7c0104b2005-11-09 04:45:33 +00002491 continue;
Chris Lattner1c08c712005-01-07 07:47:53 +00002492 }
Chris Lattner7c0104b2005-11-09 04:45:33 +00002493
2494 // N = N + Idx * ElementSize;
Dale Johannesena7ac2bd2007-10-01 23:08:35 +00002495 uint64_t ElementSize = TD->getABITypeSize(Ty);
Chris Lattner7c0104b2005-11-09 04:45:33 +00002496 SDOperand IdxN = getValue(Idx);
2497
2498 // If the index is smaller or larger than intptr_t, truncate or extend
2499 // it.
2500 if (IdxN.getValueType() < N.getValueType()) {
Reid Spencer47857812006-12-31 05:55:36 +00002501 IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
Chris Lattner7c0104b2005-11-09 04:45:33 +00002502 } else if (IdxN.getValueType() > N.getValueType())
2503 IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
2504
2505 // If this is a multiply by a power of two, turn it into a shl
2506 // immediately. This is a very common case.
2507 if (isPowerOf2_64(ElementSize)) {
2508 unsigned Amt = Log2_64(ElementSize);
2509 IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
Chris Lattner6b2d6962005-11-09 16:50:40 +00002510 DAG.getConstant(Amt, TLI.getShiftAmountTy()));
Chris Lattner7c0104b2005-11-09 04:45:33 +00002511 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
2512 continue;
2513 }
2514
Chris Lattner0bd48932008-01-17 07:00:52 +00002515 SDOperand Scale = DAG.getIntPtrConstant(ElementSize);
Chris Lattner7c0104b2005-11-09 04:45:33 +00002516 IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
2517 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
Chris Lattner1c08c712005-01-07 07:47:53 +00002518 }
2519 }
2520 setValue(&I, N);
2521}
2522
2523void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2524 // If this is a fixed sized alloca in the entry block of the function,
2525 // allocate it statically on the stack.
2526 if (FuncInfo.StaticAllocaMap.count(&I))
2527 return; // getValue will auto-populate this.
2528
2529 const Type *Ty = I.getAllocatedType();
Duncan Sands514ab342007-11-01 20:53:16 +00002530 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00002531 unsigned Align =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00002532 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
Chris Lattner58092e32007-01-20 22:35:55 +00002533 I.getAlignment());
Chris Lattner1c08c712005-01-07 07:47:53 +00002534
2535 SDOperand AllocSize = getValue(I.getArraySize());
Chris Lattner68cd65e2005-01-22 23:04:37 +00002536 MVT::ValueType IntPtr = TLI.getPointerTy();
2537 if (IntPtr < AllocSize.getValueType())
2538 AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
2539 else if (IntPtr > AllocSize.getValueType())
2540 AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
Chris Lattner1c08c712005-01-07 07:47:53 +00002541
Chris Lattner68cd65e2005-01-22 23:04:37 +00002542 AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
Chris Lattner0bd48932008-01-17 07:00:52 +00002543 DAG.getIntPtrConstant(TySize));
Chris Lattner1c08c712005-01-07 07:47:53 +00002544
Evan Cheng45157792007-08-16 23:46:29 +00002545 // Handle alignment. If the requested alignment is less than or equal to
2546 // the stack alignment, ignore it. If the size is greater than or equal to
2547 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Chris Lattner1c08c712005-01-07 07:47:53 +00002548 unsigned StackAlign =
2549 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
Evan Cheng45157792007-08-16 23:46:29 +00002550 if (Align <= StackAlign)
Chris Lattner1c08c712005-01-07 07:47:53 +00002551 Align = 0;
Evan Cheng45157792007-08-16 23:46:29 +00002552
2553 // Round the size of the allocation up to the stack alignment size
2554 // by add SA-1 to the size.
2555 AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
Chris Lattner0bd48932008-01-17 07:00:52 +00002556 DAG.getIntPtrConstant(StackAlign-1));
Evan Cheng45157792007-08-16 23:46:29 +00002557 // Mask out the low bits for alignment purposes.
2558 AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
Chris Lattner0bd48932008-01-17 07:00:52 +00002559 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Chris Lattner1c08c712005-01-07 07:47:53 +00002560
Chris Lattner0bd48932008-01-17 07:00:52 +00002561 SDOperand Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00002562 const MVT::ValueType *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(),
2563 MVT::Other);
2564 SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3);
Chris Lattner0da331f2007-02-04 01:31:47 +00002565 setValue(&I, DSA);
2566 DAG.setRoot(DSA.getValue(1));
Chris Lattner1c08c712005-01-07 07:47:53 +00002567
2568 // Inform the Frame Information that we have just allocated a variable-sized
2569 // object.
2570 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
2571}
2572
Chris Lattner1c08c712005-01-07 07:47:53 +00002573void SelectionDAGLowering::visitLoad(LoadInst &I) {
2574 SDOperand Ptr = getValue(I.getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002575
Chris Lattnerd3948112005-01-17 22:19:26 +00002576 SDOperand Root;
2577 if (I.isVolatile())
2578 Root = getRoot();
2579 else {
2580 // Do not serialize non-volatile loads against each other.
2581 Root = DAG.getRoot();
2582 }
Chris Lattner28b5b1c2006-03-15 22:19:46 +00002583
Evan Cheng466685d2006-10-09 20:57:25 +00002584 setValue(&I, getLoadFrom(I.getType(), Ptr, I.getOperand(0),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002585 Root, I.isVolatile(), I.getAlignment()));
Chris Lattner28b5b1c2006-03-15 22:19:46 +00002586}
2587
2588SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002589 const Value *SV, SDOperand Root,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002590 bool isVolatile,
2591 unsigned Alignment) {
Dan Gohman7f321562007-06-25 16:23:39 +00002592 SDOperand L =
2593 DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SV, 0,
2594 isVolatile, Alignment);
Chris Lattnerd3948112005-01-17 22:19:26 +00002595
Chris Lattner28b5b1c2006-03-15 22:19:46 +00002596 if (isVolatile)
Chris Lattnerd3948112005-01-17 22:19:26 +00002597 DAG.setRoot(L.getValue(1));
2598 else
2599 PendingLoads.push_back(L.getValue(1));
Chris Lattner28b5b1c2006-03-15 22:19:46 +00002600
2601 return L;
Chris Lattner1c08c712005-01-07 07:47:53 +00002602}
2603
2604
2605void SelectionDAGLowering::visitStore(StoreInst &I) {
2606 Value *SrcV = I.getOperand(0);
2607 SDOperand Src = getValue(SrcV);
2608 SDOperand Ptr = getValue(I.getOperand(1));
Evan Cheng0b4f80e2006-12-20 01:27:29 +00002609 DAG.setRoot(DAG.getStore(getRoot(), Src, Ptr, I.getOperand(1), 0,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002610 I.isVolatile(), I.getAlignment()));
Chris Lattner1c08c712005-01-07 07:47:53 +00002611}
2612
Chris Lattner0eade312006-03-24 02:22:33 +00002613/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2614/// node.
2615void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
2616 unsigned Intrinsic) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002617 bool HasChain = !I.doesNotAccessMemory();
2618 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2619
Chris Lattner0eade312006-03-24 02:22:33 +00002620 // Build the operand list.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002621 SmallVector<SDOperand, 8> Ops;
Chris Lattnere58a7802006-04-02 03:41:14 +00002622 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2623 if (OnlyLoad) {
2624 // We don't need to serialize loads against other loads.
2625 Ops.push_back(DAG.getRoot());
2626 } else {
2627 Ops.push_back(getRoot());
2628 }
2629 }
Chris Lattner0eade312006-03-24 02:22:33 +00002630
2631 // Add the intrinsic ID as an integer operand.
2632 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
2633
2634 // Add all operands of the call to the operand list.
2635 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2636 SDOperand Op = getValue(I.getOperand(i));
Chris Lattner0eade312006-03-24 02:22:33 +00002637 assert(TLI.isTypeLegal(Op.getValueType()) &&
2638 "Intrinsic uses a non-legal type?");
2639 Ops.push_back(Op);
2640 }
2641
2642 std::vector<MVT::ValueType> VTs;
2643 if (I.getType() != Type::VoidTy) {
2644 MVT::ValueType VT = TLI.getValueType(I.getType());
Dan Gohman7f321562007-06-25 16:23:39 +00002645 if (MVT::isVector(VT)) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002646 const VectorType *DestTy = cast<VectorType>(I.getType());
Chris Lattner0eade312006-03-24 02:22:33 +00002647 MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType());
2648
2649 VT = MVT::getVectorType(EltVT, DestTy->getNumElements());
2650 assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
2651 }
2652
2653 assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
2654 VTs.push_back(VT);
2655 }
2656 if (HasChain)
2657 VTs.push_back(MVT::Other);
2658
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00002659 const MVT::ValueType *VTList = DAG.getNodeValueTypes(VTs);
2660
Chris Lattner0eade312006-03-24 02:22:33 +00002661 // Create the node.
Chris Lattner48b61a72006-03-28 00:40:33 +00002662 SDOperand Result;
2663 if (!HasChain)
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00002664 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(),
2665 &Ops[0], Ops.size());
Chris Lattner48b61a72006-03-28 00:40:33 +00002666 else if (I.getType() != Type::VoidTy)
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00002667 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(),
2668 &Ops[0], Ops.size());
Chris Lattner48b61a72006-03-28 00:40:33 +00002669 else
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00002670 Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(),
2671 &Ops[0], Ops.size());
Chris Lattner48b61a72006-03-28 00:40:33 +00002672
Chris Lattnere58a7802006-04-02 03:41:14 +00002673 if (HasChain) {
2674 SDOperand Chain = Result.getValue(Result.Val->getNumValues()-1);
2675 if (OnlyLoad)
2676 PendingLoads.push_back(Chain);
2677 else
2678 DAG.setRoot(Chain);
2679 }
Chris Lattner0eade312006-03-24 02:22:33 +00002680 if (I.getType() != Type::VoidTy) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002681 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Dan Gohman7f321562007-06-25 16:23:39 +00002682 MVT::ValueType VT = TLI.getValueType(PTy);
2683 Result = DAG.getNode(ISD::BIT_CONVERT, VT, Result);
Chris Lattner0eade312006-03-24 02:22:33 +00002684 }
2685 setValue(&I, Result);
2686 }
2687}
2688
Duncan Sandsb4fd45e2007-07-06 09:10:03 +00002689/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002690static GlobalVariable *ExtractTypeInfo (Value *V) {
Duncan Sandsb4fd45e2007-07-06 09:10:03 +00002691 V = IntrinsicInst::StripPointerCasts(V);
2692 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002693 assert ((GV || isa<ConstantPointerNull>(V)) &&
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002694 "TypeInfo must be a global variable or NULL");
2695 return GV;
2696}
2697
Duncan Sandsf4070822007-06-15 19:04:19 +00002698/// addCatchInfo - Extract the personality and type infos from an eh.selector
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002699/// call, and add them to the specified machine basic block.
Duncan Sandsf4070822007-06-15 19:04:19 +00002700static void addCatchInfo(CallInst &I, MachineModuleInfo *MMI,
2701 MachineBasicBlock *MBB) {
2702 // Inform the MachineModuleInfo of the personality for this landing pad.
2703 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
2704 assert(CE->getOpcode() == Instruction::BitCast &&
2705 isa<Function>(CE->getOperand(0)) &&
2706 "Personality should be a function");
2707 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
2708
2709 // Gather all the type infos for this landing pad and pass them along to
2710 // MachineModuleInfo.
2711 std::vector<GlobalVariable *> TyInfo;
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002712 unsigned N = I.getNumOperands();
2713
2714 for (unsigned i = N - 1; i > 2; --i) {
2715 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
2716 unsigned FilterLength = CI->getZExtValue();
Duncan Sands6590b042007-08-27 15:47:50 +00002717 unsigned FirstCatch = i + FilterLength + !FilterLength;
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002718 assert (FirstCatch <= N && "Invalid filter length");
2719
2720 if (FirstCatch < N) {
2721 TyInfo.reserve(N - FirstCatch);
2722 for (unsigned j = FirstCatch; j < N; ++j)
2723 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2724 MMI->addCatchTypeInfo(MBB, TyInfo);
2725 TyInfo.clear();
2726 }
2727
Duncan Sands6590b042007-08-27 15:47:50 +00002728 if (!FilterLength) {
2729 // Cleanup.
2730 MMI->addCleanup(MBB);
2731 } else {
2732 // Filter.
2733 TyInfo.reserve(FilterLength - 1);
2734 for (unsigned j = i + 1; j < FirstCatch; ++j)
2735 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2736 MMI->addFilterTypeInfo(MBB, TyInfo);
2737 TyInfo.clear();
2738 }
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002739
2740 N = i;
2741 }
Duncan Sandsf4070822007-06-15 19:04:19 +00002742 }
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002743
2744 if (N > 3) {
2745 TyInfo.reserve(N - 3);
2746 for (unsigned j = 3; j < N; ++j)
2747 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
Duncan Sandsf4070822007-06-15 19:04:19 +00002748 MMI->addCatchTypeInfo(MBB, TyInfo);
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002749 }
Duncan Sandsf4070822007-06-15 19:04:19 +00002750}
2751
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002752/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
2753/// we want to emit this as a call to a named external function, return the name
2754/// otherwise lower it and return null.
2755const char *
2756SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
2757 switch (Intrinsic) {
Chris Lattner0eade312006-03-24 02:22:33 +00002758 default:
2759 // By default, turn this into a target intrinsic node.
2760 visitTargetIntrinsic(I, Intrinsic);
2761 return 0;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002762 case Intrinsic::vastart: visitVAStart(I); return 0;
2763 case Intrinsic::vaend: visitVAEnd(I); return 0;
2764 case Intrinsic::vacopy: visitVACopy(I); return 0;
Nate Begemanbcc5f362007-01-29 22:58:52 +00002765 case Intrinsic::returnaddress:
2766 setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(),
2767 getValue(I.getOperand(1))));
2768 return 0;
2769 case Intrinsic::frameaddress:
2770 setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(),
2771 getValue(I.getOperand(1))));
2772 return 0;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002773 case Intrinsic::setjmp:
Anton Korobeynikovd27a2582006-12-10 23:12:42 +00002774 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002775 break;
2776 case Intrinsic::longjmp:
Anton Korobeynikovd27a2582006-12-10 23:12:42 +00002777 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002778 break;
Chris Lattner03dd4652006-03-03 00:00:25 +00002779 case Intrinsic::memcpy_i32:
Dan Gohman707e0182008-04-12 04:36:06 +00002780 case Intrinsic::memcpy_i64: {
2781 SDOperand Op1 = getValue(I.getOperand(1));
2782 SDOperand Op2 = getValue(I.getOperand(2));
2783 SDOperand Op3 = getValue(I.getOperand(3));
2784 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
2785 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
2786 I.getOperand(1), 0, I.getOperand(2), 0));
Chris Lattner03dd4652006-03-03 00:00:25 +00002787 return 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002788 }
Chris Lattner03dd4652006-03-03 00:00:25 +00002789 case Intrinsic::memset_i32:
Dan Gohman707e0182008-04-12 04:36:06 +00002790 case Intrinsic::memset_i64: {
2791 SDOperand Op1 = getValue(I.getOperand(1));
2792 SDOperand Op2 = getValue(I.getOperand(2));
2793 SDOperand Op3 = getValue(I.getOperand(3));
2794 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
2795 DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align,
2796 I.getOperand(1), 0));
Chris Lattner03dd4652006-03-03 00:00:25 +00002797 return 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002798 }
Chris Lattner03dd4652006-03-03 00:00:25 +00002799 case Intrinsic::memmove_i32:
Dan Gohman707e0182008-04-12 04:36:06 +00002800 case Intrinsic::memmove_i64: {
2801 SDOperand Op1 = getValue(I.getOperand(1));
2802 SDOperand Op2 = getValue(I.getOperand(2));
2803 SDOperand Op3 = getValue(I.getOperand(3));
2804 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
2805
2806 // If the source and destination are known to not be aliases, we can
2807 // lower memmove as memcpy.
2808 uint64_t Size = -1ULL;
2809 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
2810 Size = C->getValue();
2811 if (AA.alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
2812 AliasAnalysis::NoAlias) {
2813 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
2814 I.getOperand(1), 0, I.getOperand(2), 0));
2815 return 0;
2816 }
2817
2818 DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align,
2819 I.getOperand(1), 0, I.getOperand(2), 0));
Chris Lattner03dd4652006-03-03 00:00:25 +00002820 return 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002821 }
Chris Lattner86cb6432005-12-13 17:40:33 +00002822 case Intrinsic::dbg_stoppoint: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002823 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey43970fe2006-03-23 18:06:46 +00002824 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002825 if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002826 SDOperand Ops[5];
Chris Lattner36ce6912005-11-29 06:21:05 +00002827
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002828 Ops[0] = getRoot();
2829 Ops[1] = getValue(SPI.getLineValue());
2830 Ops[2] = getValue(SPI.getColumnValue());
Chris Lattner36ce6912005-11-29 06:21:05 +00002831
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002832 DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext());
Jim Laskeyce72b172006-02-11 01:01:30 +00002833 assert(DD && "Not a debug information descriptor");
Jim Laskey43970fe2006-03-23 18:06:46 +00002834 CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
2835
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002836 Ops[3] = DAG.getString(CompileUnit->getFileName());
2837 Ops[4] = DAG.getString(CompileUnit->getDirectory());
Jim Laskeyce72b172006-02-11 01:01:30 +00002838
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002839 DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops, 5));
Chris Lattner86cb6432005-12-13 17:40:33 +00002840 }
Jim Laskey43970fe2006-03-23 18:06:46 +00002841
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +00002842 return 0;
Chris Lattner36ce6912005-11-29 06:21:05 +00002843 }
Jim Laskey43970fe2006-03-23 18:06:46 +00002844 case Intrinsic::dbg_region_start: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002845 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey43970fe2006-03-23 18:06:46 +00002846 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002847 if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) {
2848 unsigned LabelID = MMI->RecordRegionStart(RSI.getContext());
Jim Laskey1ee29252007-01-26 14:34:52 +00002849 DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(),
Evan Chengbb81d972008-01-31 09:59:15 +00002850 DAG.getConstant(LabelID, MVT::i32),
2851 DAG.getConstant(0, MVT::i32)));
Jim Laskey43970fe2006-03-23 18:06:46 +00002852 }
2853
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +00002854 return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00002855 }
2856 case Intrinsic::dbg_region_end: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002857 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey43970fe2006-03-23 18:06:46 +00002858 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002859 if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) {
2860 unsigned LabelID = MMI->RecordRegionEnd(REI.getContext());
Evan Chengbb81d972008-01-31 09:59:15 +00002861 DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(),
2862 DAG.getConstant(LabelID, MVT::i32),
2863 DAG.getConstant(0, MVT::i32)));
Jim Laskey43970fe2006-03-23 18:06:46 +00002864 }
2865
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +00002866 return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00002867 }
2868 case Intrinsic::dbg_func_start: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002869 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002870 if (!MMI) return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00002871 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002872 Value *SP = FSI.getSubprogram();
2873 if (SP && MMI->Verify(SP)) {
2874 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
2875 // what (most?) gdb expects.
2876 DebugInfoDesc *DD = MMI->getDescFor(SP);
2877 assert(DD && "Not a debug information descriptor");
2878 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
2879 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
2880 unsigned SrcFile = MMI->RecordSource(CompileUnit->getDirectory(),
2881 CompileUnit->getFileName());
2882 // Record the source line but does create a label. It will be emitted
2883 // at asm emission time.
2884 MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
Jim Laskey43970fe2006-03-23 18:06:46 +00002885 }
2886
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +00002887 return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00002888 }
2889 case Intrinsic::dbg_declare: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002890 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey43970fe2006-03-23 18:06:46 +00002891 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Evan Chenga844bde2008-02-02 04:07:54 +00002892 Value *Variable = DI.getVariable();
2893 if (MMI && Variable && MMI->Verify(Variable))
2894 DAG.setRoot(DAG.getNode(ISD::DECLARE, MVT::Other, getRoot(),
2895 getValue(DI.getAddress()), getValue(Variable)));
Jim Laskey43970fe2006-03-23 18:06:46 +00002896 return 0;
2897 }
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002898
Jim Laskeyb180aa12007-02-21 22:53:45 +00002899 case Intrinsic::eh_exception: {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002900 if (!CurMBB->isLandingPad()) {
2901 // FIXME: Mark exception register as live in. Hack for PR1508.
2902 unsigned Reg = TLI.getExceptionAddressRegister();
2903 if (Reg) CurMBB->addLiveIn(Reg);
Jim Laskey735b6f82007-02-22 15:38:06 +00002904 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002905 // Insert the EXCEPTIONADDR instruction.
2906 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
2907 SDOperand Ops[1];
2908 Ops[0] = DAG.getRoot();
2909 SDOperand Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1);
2910 setValue(&I, Op);
2911 DAG.setRoot(Op.getValue(1));
Jim Laskeyb180aa12007-02-21 22:53:45 +00002912 return 0;
2913 }
2914
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002915 case Intrinsic::eh_selector_i32:
2916 case Intrinsic::eh_selector_i64: {
Jim Laskeyb180aa12007-02-21 22:53:45 +00002917 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002918 MVT::ValueType VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
2919 MVT::i32 : MVT::i64);
2920
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002921 if (MMI) {
Duncan Sandsf4070822007-06-15 19:04:19 +00002922 if (CurMBB->isLandingPad())
2923 addCatchInfo(I, MMI, CurMBB);
Evan Chenge47c3332007-06-27 18:45:32 +00002924 else {
Duncan Sandsf4070822007-06-15 19:04:19 +00002925#ifndef NDEBUG
Duncan Sandsf4070822007-06-15 19:04:19 +00002926 FuncInfo.CatchInfoLost.insert(&I);
2927#endif
Duncan Sands90291952007-07-06 09:18:59 +00002928 // FIXME: Mark exception selector register as live in. Hack for PR1508.
2929 unsigned Reg = TLI.getExceptionSelectorRegister();
2930 if (Reg) CurMBB->addLiveIn(Reg);
Evan Chenge47c3332007-06-27 18:45:32 +00002931 }
Jim Laskey735b6f82007-02-22 15:38:06 +00002932
2933 // Insert the EHSELECTION instruction.
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002934 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
Jim Laskey735b6f82007-02-22 15:38:06 +00002935 SDOperand Ops[2];
2936 Ops[0] = getValue(I.getOperand(1));
2937 Ops[1] = getRoot();
2938 SDOperand Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2);
2939 setValue(&I, Op);
2940 DAG.setRoot(Op.getValue(1));
Jim Laskey7a1de982007-02-24 09:45:44 +00002941 } else {
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002942 setValue(&I, DAG.getConstant(0, VT));
Jim Laskey735b6f82007-02-22 15:38:06 +00002943 }
Jim Laskeyb180aa12007-02-21 22:53:45 +00002944
2945 return 0;
2946 }
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002947
2948 case Intrinsic::eh_typeid_for_i32:
2949 case Intrinsic::eh_typeid_for_i64: {
Jim Laskeyb180aa12007-02-21 22:53:45 +00002950 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002951 MVT::ValueType VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ?
2952 MVT::i32 : MVT::i64);
Jim Laskeyb180aa12007-02-21 22:53:45 +00002953
Jim Laskey735b6f82007-02-22 15:38:06 +00002954 if (MMI) {
2955 // Find the type id for the given typeinfo.
Duncan Sandscf26d7c2007-07-04 20:52:51 +00002956 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Duncan Sands3b346362007-05-04 17:12:26 +00002957
Jim Laskey735b6f82007-02-22 15:38:06 +00002958 unsigned TypeID = MMI->getTypeIDFor(GV);
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002959 setValue(&I, DAG.getConstant(TypeID, VT));
Jim Laskey7a1de982007-02-24 09:45:44 +00002960 } else {
Duncan Sandsf664e412007-07-06 14:46:23 +00002961 // Return something different to eh_selector.
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +00002962 setValue(&I, DAG.getConstant(1, VT));
Jim Laskey735b6f82007-02-22 15:38:06 +00002963 }
Jim Laskeyb180aa12007-02-21 22:53:45 +00002964
2965 return 0;
2966 }
2967
Anton Korobeynikov2365f512007-07-14 14:06:15 +00002968 case Intrinsic::eh_return: {
2969 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
2970
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002971 if (MMI) {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00002972 MMI->setCallsEHReturn(true);
2973 DAG.setRoot(DAG.getNode(ISD::EH_RETURN,
2974 MVT::Other,
Dan Gohman86e1ebf2008-03-27 19:56:19 +00002975 getControlRoot(),
Anton Korobeynikov2365f512007-07-14 14:06:15 +00002976 getValue(I.getOperand(1)),
2977 getValue(I.getOperand(2))));
2978 } else {
2979 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
2980 }
2981
2982 return 0;
2983 }
2984
2985 case Intrinsic::eh_unwind_init: {
2986 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
2987 MMI->setCallsUnwindInit(true);
2988 }
2989
2990 return 0;
2991 }
2992
2993 case Intrinsic::eh_dwarf_cfa: {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002994 MVT::ValueType VT = getValue(I.getOperand(1)).getValueType();
2995 SDOperand CfaArg;
2996 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getPointerTy()))
2997 CfaArg = DAG.getNode(ISD::TRUNCATE,
2998 TLI.getPointerTy(), getValue(I.getOperand(1)));
2999 else
3000 CfaArg = DAG.getNode(ISD::SIGN_EXTEND,
3001 TLI.getPointerTy(), getValue(I.getOperand(1)));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003002
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003003 SDOperand Offset = DAG.getNode(ISD::ADD,
3004 TLI.getPointerTy(),
3005 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET,
3006 TLI.getPointerTy()),
3007 CfaArg);
3008 setValue(&I, DAG.getNode(ISD::ADD,
3009 TLI.getPointerTy(),
3010 DAG.getNode(ISD::FRAMEADDR,
3011 TLI.getPointerTy(),
3012 DAG.getConstant(0,
3013 TLI.getPointerTy())),
3014 Offset));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003015 return 0;
3016 }
3017
Dale Johannesen9ab7fb32007-10-02 17:43:59 +00003018 case Intrinsic::sqrt:
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003019 setValue(&I, DAG.getNode(ISD::FSQRT,
3020 getValue(I.getOperand(1)).getValueType(),
3021 getValue(I.getOperand(1))));
3022 return 0;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +00003023 case Intrinsic::powi:
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003024 setValue(&I, DAG.getNode(ISD::FPOWI,
3025 getValue(I.getOperand(1)).getValueType(),
3026 getValue(I.getOperand(1)),
3027 getValue(I.getOperand(2))));
3028 return 0;
Dan Gohmanac9385a2007-10-12 00:01:22 +00003029 case Intrinsic::sin:
3030 setValue(&I, DAG.getNode(ISD::FSIN,
3031 getValue(I.getOperand(1)).getValueType(),
3032 getValue(I.getOperand(1))));
3033 return 0;
3034 case Intrinsic::cos:
3035 setValue(&I, DAG.getNode(ISD::FCOS,
3036 getValue(I.getOperand(1)).getValueType(),
3037 getValue(I.getOperand(1))));
3038 return 0;
3039 case Intrinsic::pow:
3040 setValue(&I, DAG.getNode(ISD::FPOW,
3041 getValue(I.getOperand(1)).getValueType(),
3042 getValue(I.getOperand(1)),
3043 getValue(I.getOperand(2))));
3044 return 0;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003045 case Intrinsic::pcmarker: {
3046 SDOperand Tmp = getValue(I.getOperand(1));
3047 DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
3048 return 0;
3049 }
Andrew Lenharth8b91c772005-11-11 22:48:54 +00003050 case Intrinsic::readcyclecounter: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003051 SDOperand Op = getRoot();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003052 SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER,
3053 DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2,
3054 &Op, 1);
Andrew Lenharth8b91c772005-11-11 22:48:54 +00003055 setValue(&I, Tmp);
3056 DAG.setRoot(Tmp.getValue(1));
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003057 return 0;
Andrew Lenharth8b91c772005-11-11 22:48:54 +00003058 }
Chris Lattnerc6eb6d72007-04-10 03:20:39 +00003059 case Intrinsic::part_select: {
Reid Spencer3f108cb2007-04-05 01:20:18 +00003060 // Currently not implemented: just abort
Reid Spencerf75b8742007-04-12 02:48:46 +00003061 assert(0 && "part_select intrinsic not implemented");
3062 abort();
3063 }
3064 case Intrinsic::part_set: {
3065 // Currently not implemented: just abort
3066 assert(0 && "part_set intrinsic not implemented");
Reid Spencer3f108cb2007-04-05 01:20:18 +00003067 abort();
Reid Spenceraddd11d2007-04-04 23:48:25 +00003068 }
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003069 case Intrinsic::bswap:
Nate Begemand88fc032006-01-14 03:14:10 +00003070 setValue(&I, DAG.getNode(ISD::BSWAP,
3071 getValue(I.getOperand(1)).getValueType(),
3072 getValue(I.getOperand(1))));
3073 return 0;
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003074 case Intrinsic::cttz: {
3075 SDOperand Arg = getValue(I.getOperand(1));
3076 MVT::ValueType Ty = Arg.getValueType();
3077 SDOperand result = DAG.getNode(ISD::CTTZ, Ty, Arg);
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003078 setValue(&I, result);
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003079 return 0;
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003080 }
3081 case Intrinsic::ctlz: {
3082 SDOperand Arg = getValue(I.getOperand(1));
3083 MVT::ValueType Ty = Arg.getValueType();
3084 SDOperand result = DAG.getNode(ISD::CTLZ, Ty, Arg);
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003085 setValue(&I, result);
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003086 return 0;
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003087 }
3088 case Intrinsic::ctpop: {
3089 SDOperand Arg = getValue(I.getOperand(1));
3090 MVT::ValueType Ty = Arg.getValueType();
3091 SDOperand result = DAG.getNode(ISD::CTPOP, Ty, Arg);
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003092 setValue(&I, result);
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003093 return 0;
Reid Spencera4f9c4d2007-04-01 07:34:11 +00003094 }
Chris Lattner140d53c2006-01-13 02:50:02 +00003095 case Intrinsic::stacksave: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003096 SDOperand Op = getRoot();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003097 SDOperand Tmp = DAG.getNode(ISD::STACKSAVE,
3098 DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1);
Chris Lattner140d53c2006-01-13 02:50:02 +00003099 setValue(&I, Tmp);
3100 DAG.setRoot(Tmp.getValue(1));
3101 return 0;
3102 }
Chris Lattner39a17dd2006-01-23 05:22:07 +00003103 case Intrinsic::stackrestore: {
3104 SDOperand Tmp = getValue(I.getOperand(1));
3105 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
Chris Lattner140d53c2006-01-13 02:50:02 +00003106 return 0;
Chris Lattner39a17dd2006-01-23 05:22:07 +00003107 }
Tanya Lattner24e5aad2007-06-15 22:26:58 +00003108 case Intrinsic::var_annotation:
3109 // Discard annotate attributes
3110 return 0;
Duncan Sands36397f52007-07-27 12:58:54 +00003111
Duncan Sands36397f52007-07-27 12:58:54 +00003112 case Intrinsic::init_trampoline: {
3113 const Function *F =
3114 cast<Function>(IntrinsicInst::StripPointerCasts(I.getOperand(2)));
3115
3116 SDOperand Ops[6];
3117 Ops[0] = getRoot();
3118 Ops[1] = getValue(I.getOperand(1));
3119 Ops[2] = getValue(I.getOperand(2));
3120 Ops[3] = getValue(I.getOperand(3));
3121 Ops[4] = DAG.getSrcValue(I.getOperand(1));
3122 Ops[5] = DAG.getSrcValue(F);
3123
Duncan Sandsf7331b32007-09-11 14:10:23 +00003124 SDOperand Tmp = DAG.getNode(ISD::TRAMPOLINE,
3125 DAG.getNodeValueTypes(TLI.getPointerTy(),
3126 MVT::Other), 2,
3127 Ops, 6);
3128
3129 setValue(&I, Tmp);
3130 DAG.setRoot(Tmp.getValue(1));
Duncan Sands36397f52007-07-27 12:58:54 +00003131 return 0;
3132 }
Gordon Henriksence224772008-01-07 01:30:38 +00003133
3134 case Intrinsic::gcroot:
3135 if (GCI) {
3136 Value *Alloca = I.getOperand(1);
3137 Constant *TypeMap = cast<Constant>(I.getOperand(2));
3138
3139 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).Val);
3140 GCI->addStackRoot(FI->getIndex(), TypeMap);
3141 }
3142 return 0;
3143
3144 case Intrinsic::gcread:
3145 case Intrinsic::gcwrite:
3146 assert(0 && "Collector failed to lower gcread/gcwrite intrinsics!");
3147 return 0;
3148
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003149 case Intrinsic::flt_rounds: {
Dan Gohman1a024862008-01-31 00:41:03 +00003150 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, MVT::i32));
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003151 return 0;
3152 }
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003153
3154 case Intrinsic::trap: {
3155 DAG.setRoot(DAG.getNode(ISD::TRAP, MVT::Other, getRoot()));
3156 return 0;
3157 }
Evan Cheng27b7db52008-03-08 00:58:38 +00003158 case Intrinsic::prefetch: {
3159 SDOperand Ops[4];
3160 Ops[0] = getRoot();
3161 Ops[1] = getValue(I.getOperand(1));
3162 Ops[2] = getValue(I.getOperand(2));
3163 Ops[3] = getValue(I.getOperand(3));
3164 DAG.setRoot(DAG.getNode(ISD::PREFETCH, MVT::Other, &Ops[0], 4));
3165 return 0;
3166 }
3167
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00003168 case Intrinsic::memory_barrier: {
3169 SDOperand Ops[6];
3170 Ops[0] = getRoot();
3171 for (int x = 1; x < 6; ++x)
3172 Ops[x] = getValue(I.getOperand(x));
3173
3174 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, MVT::Other, &Ops[0], 6));
3175 return 0;
3176 }
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003177 case Intrinsic::atomic_lcs: {
3178 SDOperand Root = getRoot();
3179 SDOperand O3 = getValue(I.getOperand(3));
3180 SDOperand L = DAG.getAtomic(ISD::ATOMIC_LCS, Root,
3181 getValue(I.getOperand(1)),
3182 getValue(I.getOperand(2)),
3183 O3, O3.getValueType());
3184 setValue(&I, L);
3185 DAG.setRoot(L.getValue(1));
3186 return 0;
3187 }
3188 case Intrinsic::atomic_las: {
3189 SDOperand Root = getRoot();
3190 SDOperand O2 = getValue(I.getOperand(2));
3191 SDOperand L = DAG.getAtomic(ISD::ATOMIC_LAS, Root,
3192 getValue(I.getOperand(1)),
3193 O2, O2.getValueType());
3194 setValue(&I, L);
3195 DAG.setRoot(L.getValue(1));
3196 return 0;
3197 }
3198 case Intrinsic::atomic_swap: {
3199 SDOperand Root = getRoot();
3200 SDOperand O2 = getValue(I.getOperand(2));
3201 SDOperand L = DAG.getAtomic(ISD::ATOMIC_SWAP, Root,
3202 getValue(I.getOperand(1)),
3203 O2, O2.getValueType());
3204 setValue(&I, L);
3205 DAG.setRoot(L.getValue(1));
3206 return 0;
3207 }
3208
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003209 }
3210}
3211
3212
Duncan Sands6f74b482007-12-19 09:48:52 +00003213void SelectionDAGLowering::LowerCallTo(CallSite CS, SDOperand Callee,
Jim Laskey1da20a72007-02-23 21:45:01 +00003214 bool IsTailCall,
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003215 MachineBasicBlock *LandingPad) {
Duncan Sands6f74b482007-12-19 09:48:52 +00003216 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
Jim Laskey735b6f82007-02-22 15:38:06 +00003217 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003218 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3219 unsigned BeginLabel = 0, EndLabel = 0;
Duncan Sands6f74b482007-12-19 09:48:52 +00003220
Jim Laskey735b6f82007-02-22 15:38:06 +00003221 TargetLowering::ArgListTy Args;
3222 TargetLowering::ArgListEntry Entry;
Duncan Sands6f74b482007-12-19 09:48:52 +00003223 Args.reserve(CS.arg_size());
3224 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
3225 i != e; ++i) {
3226 SDOperand ArgNode = getValue(*i);
3227 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
Duncan Sands4fee7032007-05-07 20:49:28 +00003228
Duncan Sands6f74b482007-12-19 09:48:52 +00003229 unsigned attrInd = i - CS.arg_begin() + 1;
3230 Entry.isSExt = CS.paramHasAttr(attrInd, ParamAttr::SExt);
3231 Entry.isZExt = CS.paramHasAttr(attrInd, ParamAttr::ZExt);
3232 Entry.isInReg = CS.paramHasAttr(attrInd, ParamAttr::InReg);
3233 Entry.isSRet = CS.paramHasAttr(attrInd, ParamAttr::StructRet);
3234 Entry.isNest = CS.paramHasAttr(attrInd, ParamAttr::Nest);
3235 Entry.isByVal = CS.paramHasAttr(attrInd, ParamAttr::ByVal);
Dale Johannesen08e78b12008-02-22 17:49:45 +00003236 Entry.Alignment = CS.getParamAlignment(attrInd);
Jim Laskey735b6f82007-02-22 15:38:06 +00003237 Args.push_back(Entry);
3238 }
3239
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003240 if (LandingPad && MMI) {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003241 // Insert a label before the invoke call to mark the try range. This can be
3242 // used to detect deletion of the invoke via the MachineModuleInfo.
3243 BeginLabel = MMI->NextLabelID();
Dale Johannesena4091d32008-04-04 23:48:31 +00003244 // Both PendingLoads and PendingExports must be flushed here;
3245 // this call might not return.
3246 (void)getRoot();
3247 DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getControlRoot(),
Evan Chengbb81d972008-01-31 09:59:15 +00003248 DAG.getConstant(BeginLabel, MVT::i32),
3249 DAG.getConstant(1, MVT::i32)));
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003250 }
Duncan Sands6f74b482007-12-19 09:48:52 +00003251
Jim Laskey735b6f82007-02-22 15:38:06 +00003252 std::pair<SDOperand,SDOperand> Result =
Duncan Sands6f74b482007-12-19 09:48:52 +00003253 TLI.LowerCallTo(getRoot(), CS.getType(),
3254 CS.paramHasAttr(0, ParamAttr::SExt),
Duncan Sands00fee652008-02-14 17:28:50 +00003255 CS.paramHasAttr(0, ParamAttr::ZExt),
Duncan Sands6f74b482007-12-19 09:48:52 +00003256 FTy->isVarArg(), CS.getCallingConv(), IsTailCall,
Jim Laskey735b6f82007-02-22 15:38:06 +00003257 Callee, Args, DAG);
Duncan Sands6f74b482007-12-19 09:48:52 +00003258 if (CS.getType() != Type::VoidTy)
3259 setValue(CS.getInstruction(), Result.first);
Jim Laskey735b6f82007-02-22 15:38:06 +00003260 DAG.setRoot(Result.second);
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003261
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003262 if (LandingPad && MMI) {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003263 // Insert a label at the end of the invoke call to mark the try range. This
3264 // can be used to detect deletion of the invoke via the MachineModuleInfo.
3265 EndLabel = MMI->NextLabelID();
3266 DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(),
Evan Chengbb81d972008-01-31 09:59:15 +00003267 DAG.getConstant(EndLabel, MVT::i32),
3268 DAG.getConstant(1, MVT::i32)));
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003269
Duncan Sands6f74b482007-12-19 09:48:52 +00003270 // Inform MachineModuleInfo of range.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003271 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
3272 }
Jim Laskey735b6f82007-02-22 15:38:06 +00003273}
3274
3275
Chris Lattner1c08c712005-01-07 07:47:53 +00003276void SelectionDAGLowering::visitCall(CallInst &I) {
Chris Lattner64e14b12005-01-08 22:48:57 +00003277 const char *RenameFn = 0;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003278 if (Function *F = I.getCalledFunction()) {
Chris Lattner87b51bc2007-09-10 21:15:22 +00003279 if (F->isDeclaration()) {
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003280 if (unsigned IID = F->getIntrinsicID()) {
3281 RenameFn = visitIntrinsicCall(I, IID);
3282 if (!RenameFn)
3283 return;
Chris Lattner87b51bc2007-09-10 21:15:22 +00003284 }
3285 }
3286
3287 // Check for well-known libc/libm calls. If the function is internal, it
3288 // can't be a library call.
3289 unsigned NameLen = F->getNameLen();
3290 if (!F->hasInternalLinkage() && NameLen) {
3291 const char *NameStr = F->getNameStart();
3292 if (NameStr[0] == 'c' &&
3293 ((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
3294 (NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
3295 if (I.getNumOperands() == 3 && // Basic sanity checks.
3296 I.getOperand(1)->getType()->isFloatingPoint() &&
3297 I.getType() == I.getOperand(1)->getType() &&
3298 I.getType() == I.getOperand(2)->getType()) {
3299 SDOperand LHS = getValue(I.getOperand(1));
3300 SDOperand RHS = getValue(I.getOperand(2));
3301 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
3302 LHS, RHS));
3303 return;
3304 }
3305 } else if (NameStr[0] == 'f' &&
3306 ((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
Dale Johannesen2f429012007-09-26 21:10:55 +00003307 (NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
3308 (NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
Chris Lattner87b51bc2007-09-10 21:15:22 +00003309 if (I.getNumOperands() == 2 && // Basic sanity checks.
3310 I.getOperand(1)->getType()->isFloatingPoint() &&
3311 I.getType() == I.getOperand(1)->getType()) {
3312 SDOperand Tmp = getValue(I.getOperand(1));
3313 setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
3314 return;
3315 }
3316 } else if (NameStr[0] == 's' &&
3317 ((NameLen == 3 && !strcmp(NameStr, "sin")) ||
Dale Johannesen2f429012007-09-26 21:10:55 +00003318 (NameLen == 4 && !strcmp(NameStr, "sinf")) ||
3319 (NameLen == 4 && !strcmp(NameStr, "sinl")))) {
Chris Lattner87b51bc2007-09-10 21:15:22 +00003320 if (I.getNumOperands() == 2 && // Basic sanity checks.
3321 I.getOperand(1)->getType()->isFloatingPoint() &&
3322 I.getType() == I.getOperand(1)->getType()) {
3323 SDOperand Tmp = getValue(I.getOperand(1));
3324 setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
3325 return;
3326 }
3327 } else if (NameStr[0] == 'c' &&
3328 ((NameLen == 3 && !strcmp(NameStr, "cos")) ||
Dale Johannesen2f429012007-09-26 21:10:55 +00003329 (NameLen == 4 && !strcmp(NameStr, "cosf")) ||
3330 (NameLen == 4 && !strcmp(NameStr, "cosl")))) {
Chris Lattner87b51bc2007-09-10 21:15:22 +00003331 if (I.getNumOperands() == 2 && // Basic sanity checks.
3332 I.getOperand(1)->getType()->isFloatingPoint() &&
3333 I.getType() == I.getOperand(1)->getType()) {
3334 SDOperand Tmp = getValue(I.getOperand(1));
3335 setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
3336 return;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003337 }
Chris Lattner1ca85d52005-05-14 13:56:55 +00003338 }
Chris Lattner87b51bc2007-09-10 21:15:22 +00003339 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00003340 } else if (isa<InlineAsm>(I.getOperand(0))) {
Duncan Sandsfd7b3262007-12-17 18:08:19 +00003341 visitInlineAsm(&I);
Chris Lattnerce7518c2006-01-26 22:24:51 +00003342 return;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00003343 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003344
Chris Lattner64e14b12005-01-08 22:48:57 +00003345 SDOperand Callee;
3346 if (!RenameFn)
3347 Callee = getValue(I.getOperand(0));
3348 else
3349 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003350
Duncan Sands6f74b482007-12-19 09:48:52 +00003351 LowerCallTo(&I, Callee, I.isTailCall());
Chris Lattner1c08c712005-01-07 07:47:53 +00003352}
3353
Jim Laskey735b6f82007-02-22 15:38:06 +00003354
Dan Gohmanef5d1942008-03-11 21:11:25 +00003355void SelectionDAGLowering::visitGetResult(GetResultInst &I) {
Dan Gohman67780f12008-04-23 20:25:16 +00003356 if (isa<UndefValue>(I.getOperand(0))) {
Dan Gohman3dc34f62008-04-23 20:21:29 +00003357 SDOperand Undef = DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()));
3358 setValue(&I, Undef);
Chris Lattner6833b062008-04-28 07:16:35 +00003359 return;
Dan Gohman3dc34f62008-04-23 20:21:29 +00003360 }
Chris Lattner6833b062008-04-28 07:16:35 +00003361
3362 // To add support for individual return values with aggregate types,
3363 // we'd need a way to take a getresult index and determine which
3364 // values of the Call SDNode are associated with it.
3365 assert(TLI.getValueType(I.getType(), true) != MVT::Other &&
3366 "Individual return values must not be aggregates!");
3367
3368 SDOperand Call = getValue(I.getOperand(0));
3369 setValue(&I, SDOperand(Call.Val, I.getIndex()));
Dan Gohmanef5d1942008-03-11 21:11:25 +00003370}
3371
3372
Dan Gohmanb6f5b002007-06-28 23:29:44 +00003373/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
3374/// this value and returns the result as a ValueVT value. This uses
3375/// Chain/Flag as the input and updates them for the output Chain/Flag.
3376/// If the Flag pointer is NULL, no flag is used.
3377SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
Chris Lattner6833b062008-04-28 07:16:35 +00003378 SDOperand &Chain,
3379 SDOperand *Flag) const {
Dan Gohman23ce5022008-04-25 18:27:55 +00003380 // Assemble the legal parts into the final values.
3381 SmallVector<SDOperand, 4> Values(ValueVTs.size());
Chris Lattner6833b062008-04-28 07:16:35 +00003382 SmallVector<SDOperand, 8> Parts;
3383 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Dan Gohman23ce5022008-04-25 18:27:55 +00003384 // Copy the legal parts from the registers.
3385 MVT::ValueType ValueVT = ValueVTs[Value];
3386 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
3387 MVT::ValueType RegisterVT = RegVTs[Value];
3388
Chris Lattner6833b062008-04-28 07:16:35 +00003389 Parts.resize(NumRegs);
Dan Gohman23ce5022008-04-25 18:27:55 +00003390 for (unsigned i = 0; i != NumRegs; ++i) {
Chris Lattner6833b062008-04-28 07:16:35 +00003391 SDOperand P;
3392 if (Flag == 0)
3393 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT);
3394 else {
3395 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag);
Dan Gohman23ce5022008-04-25 18:27:55 +00003396 *Flag = P.getValue(2);
Chris Lattner6833b062008-04-28 07:16:35 +00003397 }
3398 Chain = P.getValue(1);
Dan Gohman23ce5022008-04-25 18:27:55 +00003399 Parts[Part+i] = P;
3400 }
Chris Lattner5df99b32007-03-25 05:00:54 +00003401
Dan Gohman23ce5022008-04-25 18:27:55 +00003402 Values[Value] = getCopyFromParts(DAG, &Parts[Part], NumRegs, RegisterVT,
3403 ValueVT);
3404 Part += NumRegs;
3405 }
Chris Lattner6833b062008-04-28 07:16:35 +00003406
3407 if (ValueVTs.size() == 1)
3408 return Values[0];
3409
Dan Gohman23ce5022008-04-25 18:27:55 +00003410 return DAG.getNode(ISD::MERGE_VALUES,
3411 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
3412 &Values[0], ValueVTs.size());
Chris Lattner864635a2006-02-22 22:37:12 +00003413}
3414
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003415/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
3416/// specified value into the registers specified by this object. This uses
3417/// Chain/Flag as the input and updates them for the output Chain/Flag.
Dan Gohmanb6f5b002007-06-28 23:29:44 +00003418/// If the Flag pointer is NULL, no flag is used.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003419void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
Dan Gohmanb6f5b002007-06-28 23:29:44 +00003420 SDOperand &Chain, SDOperand *Flag) const {
Dan Gohmanb6f5b002007-06-28 23:29:44 +00003421 // Get the list of the values's legal parts.
Dan Gohman23ce5022008-04-25 18:27:55 +00003422 unsigned NumRegs = Regs.size();
3423 SmallVector<SDOperand, 8> Parts(NumRegs);
Chris Lattner6833b062008-04-28 07:16:35 +00003424 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Dan Gohman23ce5022008-04-25 18:27:55 +00003425 MVT::ValueType ValueVT = ValueVTs[Value];
3426 unsigned NumParts = TLI->getNumRegisters(ValueVT);
3427 MVT::ValueType RegisterVT = RegVTs[Value];
3428
3429 getCopyToParts(DAG, Val.getValue(Val.ResNo + Value),
3430 &Parts[Part], NumParts, RegisterVT);
3431 Part += NumParts;
3432 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +00003433
3434 // Copy the parts into the registers.
Dan Gohman23ce5022008-04-25 18:27:55 +00003435 SmallVector<SDOperand, 8> Chains(NumRegs);
3436 for (unsigned i = 0; i != NumRegs; ++i) {
Chris Lattner6833b062008-04-28 07:16:35 +00003437 SDOperand Part;
3438 if (Flag == 0)
3439 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]);
3440 else {
3441 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag);
Dan Gohmanb6f5b002007-06-28 23:29:44 +00003442 *Flag = Part.getValue(1);
Chris Lattner6833b062008-04-28 07:16:35 +00003443 }
3444 Chains[i] = Part.getValue(0);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003445 }
Chris Lattner6833b062008-04-28 07:16:35 +00003446
3447 if (NumRegs == 1)
3448 Chain = Chains[0];
3449 else
3450 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumRegs);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003451}
Chris Lattner864635a2006-02-22 22:37:12 +00003452
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003453/// AddInlineAsmOperands - Add this value to the specified inlineasm node
3454/// operand list. This adds the code marker and includes the number of
3455/// values added into it.
3456void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
Chris Lattner9f6637d2006-02-23 20:06:57 +00003457 std::vector<SDOperand> &Ops) const {
Chris Lattner4b993b12007-04-09 00:33:58 +00003458 MVT::ValueType IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
3459 Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy));
Chris Lattner6833b062008-04-28 07:16:35 +00003460 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
3461 unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]);
Dan Gohman23ce5022008-04-25 18:27:55 +00003462 MVT::ValueType RegisterVT = RegVTs[Value];
Chris Lattner6833b062008-04-28 07:16:35 +00003463 for (unsigned i = 0; i != NumRegs; ++i)
3464 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Dan Gohman23ce5022008-04-25 18:27:55 +00003465 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003466}
Chris Lattner864635a2006-02-22 22:37:12 +00003467
3468/// isAllocatableRegister - If the specified register is safe to allocate,
3469/// i.e. it isn't a stack pointer or some other special register, return the
3470/// register class for the register. Otherwise, return null.
3471static const TargetRegisterClass *
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00003472isAllocatableRegister(unsigned Reg, MachineFunction &MF,
Dan Gohman6f0d0242008-02-10 18:45:23 +00003473 const TargetLowering &TLI,
3474 const TargetRegisterInfo *TRI) {
Chris Lattnerf8814cf2006-04-02 00:24:45 +00003475 MVT::ValueType FoundVT = MVT::Other;
3476 const TargetRegisterClass *FoundRC = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +00003477 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
3478 E = TRI->regclass_end(); RCI != E; ++RCI) {
Chris Lattnerf8814cf2006-04-02 00:24:45 +00003479 MVT::ValueType ThisVT = MVT::Other;
3480
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00003481 const TargetRegisterClass *RC = *RCI;
3482 // If none of the the value types for this register class are valid, we
3483 // can't use it. For example, 64-bit reg classes on 32-bit targets.
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00003484 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
3485 I != E; ++I) {
3486 if (TLI.isTypeLegal(*I)) {
Chris Lattnerf8814cf2006-04-02 00:24:45 +00003487 // If we have already found this register in a different register class,
3488 // choose the one with the largest VT specified. For example, on
3489 // PowerPC, we favor f64 register classes over f32.
3490 if (FoundVT == MVT::Other ||
3491 MVT::getSizeInBits(FoundVT) < MVT::getSizeInBits(*I)) {
3492 ThisVT = *I;
3493 break;
3494 }
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00003495 }
3496 }
3497
Chris Lattnerf8814cf2006-04-02 00:24:45 +00003498 if (ThisVT == MVT::Other) continue;
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00003499
Chris Lattner864635a2006-02-22 22:37:12 +00003500 // NOTE: This isn't ideal. In particular, this might allocate the
3501 // frame pointer in functions that need it (due to them not being taken
3502 // out of allocation, because a variable sized allocation hasn't been seen
3503 // yet). This is a slight code pessimization, but should still work.
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00003504 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
3505 E = RC->allocation_order_end(MF); I != E; ++I)
Chris Lattnerf8814cf2006-04-02 00:24:45 +00003506 if (*I == Reg) {
3507 // We found a matching register class. Keep looking at others in case
3508 // we find one with larger registers that this physreg is also in.
3509 FoundRC = RC;
3510 FoundVT = ThisVT;
3511 break;
3512 }
Chris Lattner4e4b5762006-02-01 18:59:47 +00003513 }
Chris Lattnerf8814cf2006-04-02 00:24:45 +00003514 return FoundRC;
Chris Lattner864635a2006-02-22 22:37:12 +00003515}
3516
Chris Lattner4e4b5762006-02-01 18:59:47 +00003517
Chris Lattner0c583402007-04-28 20:49:53 +00003518namespace {
3519/// AsmOperandInfo - This contains information for each constraint that we are
3520/// lowering.
Evan Cheng5c807602008-02-26 02:33:44 +00003521struct SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
3522 /// CallOperand - If this is the result output operand or a clobber
3523 /// this is null, otherwise it is the incoming operand to the CallInst.
3524 /// This gets modified as the asm is processed.
Chris Lattner0c583402007-04-28 20:49:53 +00003525 SDOperand CallOperand;
Evan Cheng5c807602008-02-26 02:33:44 +00003526
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003527 /// AssignedRegs - If this is a register or register class operand, this
3528 /// contains the set of register corresponding to the operand.
3529 RegsForValue AssignedRegs;
3530
Dan Gohman23ce5022008-04-25 18:27:55 +00003531 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
Evan Cheng5c807602008-02-26 02:33:44 +00003532 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
Chris Lattner0c583402007-04-28 20:49:53 +00003533 }
Chris Lattner3ff90dc2007-04-30 17:16:27 +00003534
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003535 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
3536 /// busy in OutputRegs/InputRegs.
3537 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
3538 std::set<unsigned> &OutputRegs,
Chris Lattner7cbeb242008-02-21 04:55:52 +00003539 std::set<unsigned> &InputRegs,
3540 const TargetRegisterInfo &TRI) const {
3541 if (isOutReg) {
3542 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
3543 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
3544 }
3545 if (isInReg) {
3546 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
3547 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
3548 }
3549 }
3550
3551private:
3552 /// MarkRegAndAliases - Mark the specified register and all aliases in the
3553 /// specified set.
3554 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
3555 const TargetRegisterInfo &TRI) {
3556 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
3557 Regs.insert(Reg);
3558 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
3559 for (; *Aliases; ++Aliases)
3560 Regs.insert(*Aliases);
3561 }
Chris Lattner0c583402007-04-28 20:49:53 +00003562};
3563} // end anon namespace.
Chris Lattner864635a2006-02-22 22:37:12 +00003564
Chris Lattner3ff90dc2007-04-30 17:16:27 +00003565
Chris Lattner0fe71e92008-02-21 19:43:13 +00003566/// GetRegistersForValue - Assign registers (virtual or physical) for the
3567/// specified operand. We prefer to assign virtual registers, to allow the
3568/// register allocator handle the assignment process. However, if the asm uses
3569/// features that we can't model on machineinstrs, we have SDISel do the
3570/// allocation. This produces generally horrible, but correct, code.
3571///
3572/// OpInfo describes the operand.
3573/// HasEarlyClobber is true if there are any early clobber constraints (=&r)
3574/// or any explicitly clobbered registers.
3575/// Input and OutputRegs are the set of already allocated physical registers.
3576///
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003577void SelectionDAGLowering::
Evan Cheng5c807602008-02-26 02:33:44 +00003578GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, bool HasEarlyClobber,
Chris Lattnerbf996f12007-04-30 17:29:31 +00003579 std::set<unsigned> &OutputRegs,
3580 std::set<unsigned> &InputRegs) {
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003581 // Compute whether this value requires an input register, an output register,
3582 // or both.
3583 bool isOutReg = false;
3584 bool isInReg = false;
3585 switch (OpInfo.Type) {
3586 case InlineAsm::isOutput:
3587 isOutReg = true;
3588
3589 // If this is an early-clobber output, or if there is an input
3590 // constraint that matches this, we need to reserve the input register
3591 // so no other inputs allocate to it.
3592 isInReg = OpInfo.isEarlyClobber || OpInfo.hasMatchingInput;
3593 break;
3594 case InlineAsm::isInput:
3595 isInReg = true;
3596 isOutReg = false;
3597 break;
3598 case InlineAsm::isClobber:
3599 isOutReg = true;
3600 isInReg = true;
3601 break;
3602 }
3603
3604
3605 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerb606dba2008-04-28 06:44:42 +00003606 SmallVector<unsigned, 4> Regs;
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003607
3608 // If this is a constraint for a single physreg, or a constraint for a
3609 // register class, find it.
3610 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
3611 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
3612 OpInfo.ConstraintVT);
Chris Lattnerbf996f12007-04-30 17:29:31 +00003613
3614 unsigned NumRegs = 1;
3615 if (OpInfo.ConstraintVT != MVT::Other)
Dan Gohmanb9f10192007-06-21 14:42:22 +00003616 NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT);
Chris Lattnerbf996f12007-04-30 17:29:31 +00003617 MVT::ValueType RegVT;
3618 MVT::ValueType ValueVT = OpInfo.ConstraintVT;
3619
Chris Lattnerbf996f12007-04-30 17:29:31 +00003620
3621 // If this is a constraint for a specific physical register, like {r17},
3622 // assign it now.
3623 if (PhysReg.first) {
3624 if (OpInfo.ConstraintVT == MVT::Other)
3625 ValueVT = *PhysReg.second->vt_begin();
3626
3627 // Get the actual register value type. This is important, because the user
3628 // may have asked for (e.g.) the AX register in i32 type. We need to
3629 // remember that AX is actually i16 to get the right extension.
3630 RegVT = *PhysReg.second->vt_begin();
3631
3632 // This is a explicit reference to a physical register.
3633 Regs.push_back(PhysReg.first);
3634
3635 // If this is an expanded reference, add the rest of the regs to Regs.
3636 if (NumRegs != 1) {
3637 TargetRegisterClass::iterator I = PhysReg.second->begin();
3638 TargetRegisterClass::iterator E = PhysReg.second->end();
3639 for (; *I != PhysReg.first; ++I)
3640 assert(I != E && "Didn't find reg!");
3641
3642 // Already added the first reg.
3643 --NumRegs; ++I;
3644 for (; NumRegs; --NumRegs, ++I) {
3645 assert(I != E && "Ran out of registers to allocate!");
3646 Regs.push_back(*I);
3647 }
3648 }
Dan Gohman23ce5022008-04-25 18:27:55 +00003649 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
Chris Lattner7cbeb242008-02-21 04:55:52 +00003650 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
3651 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003652 return;
Chris Lattnerbf996f12007-04-30 17:29:31 +00003653 }
3654
3655 // Otherwise, if this was a reference to an LLVM register class, create vregs
3656 // for this reference.
3657 std::vector<unsigned> RegClassRegs;
Chris Lattnerc2c28fc2007-06-15 19:11:01 +00003658 const TargetRegisterClass *RC = PhysReg.second;
3659 if (RC) {
Chris Lattnerbf996f12007-04-30 17:29:31 +00003660 // If this is an early clobber or tied register, our regalloc doesn't know
3661 // how to maintain the constraint. If it isn't, go ahead and create vreg
3662 // and let the regalloc do the right thing.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003663 if (!OpInfo.hasMatchingInput && !OpInfo.isEarlyClobber &&
3664 // If there is some other early clobber and this is an input register,
3665 // then we are forced to pre-allocate the input reg so it doesn't
3666 // conflict with the earlyclobber.
3667 !(OpInfo.Type == InlineAsm::isInput && HasEarlyClobber)) {
Chris Lattnerbf996f12007-04-30 17:29:31 +00003668 RegVT = *PhysReg.second->vt_begin();
3669
3670 if (OpInfo.ConstraintVT == MVT::Other)
3671 ValueVT = RegVT;
3672
3673 // Create the appropriate number of virtual registers.
Chris Lattner84bc5422007-12-31 04:13:23 +00003674 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Chris Lattnerbf996f12007-04-30 17:29:31 +00003675 for (; NumRegs; --NumRegs)
Chris Lattner84bc5422007-12-31 04:13:23 +00003676 Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second));
Chris Lattnerbf996f12007-04-30 17:29:31 +00003677
Dan Gohman23ce5022008-04-25 18:27:55 +00003678 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003679 return;
Chris Lattnerbf996f12007-04-30 17:29:31 +00003680 }
3681
3682 // Otherwise, we can't allocate it. Let the code below figure out how to
3683 // maintain these constraints.
3684 RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end());
3685
3686 } else {
3687 // This is a reference to a register class that doesn't directly correspond
3688 // to an LLVM register class. Allocate NumRegs consecutive, available,
3689 // registers from the class.
3690 RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
3691 OpInfo.ConstraintVT);
3692 }
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003693
Dan Gohman6f0d0242008-02-10 18:45:23 +00003694 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
Chris Lattnerbf996f12007-04-30 17:29:31 +00003695 unsigned NumAllocated = 0;
3696 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
3697 unsigned Reg = RegClassRegs[i];
3698 // See if this register is available.
3699 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
3700 (isInReg && InputRegs.count(Reg))) { // Already used.
3701 // Make sure we find consecutive registers.
3702 NumAllocated = 0;
3703 continue;
3704 }
3705
3706 // Check to see if this register is allocatable (i.e. don't give out the
3707 // stack pointer).
Chris Lattnerc2c28fc2007-06-15 19:11:01 +00003708 if (RC == 0) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00003709 RC = isAllocatableRegister(Reg, MF, TLI, TRI);
Chris Lattnerc2c28fc2007-06-15 19:11:01 +00003710 if (!RC) { // Couldn't allocate this register.
3711 // Reset NumAllocated to make sure we return consecutive registers.
3712 NumAllocated = 0;
3713 continue;
3714 }
Chris Lattnerbf996f12007-04-30 17:29:31 +00003715 }
3716
3717 // Okay, this register is good, we can use it.
3718 ++NumAllocated;
3719
3720 // If we allocated enough consecutive registers, succeed.
3721 if (NumAllocated == NumRegs) {
3722 unsigned RegStart = (i-NumAllocated)+1;
3723 unsigned RegEnd = i+1;
3724 // Mark all of the allocated registers used.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003725 for (unsigned i = RegStart; i != RegEnd; ++i)
3726 Regs.push_back(RegClassRegs[i]);
Chris Lattnerbf996f12007-04-30 17:29:31 +00003727
Dan Gohman23ce5022008-04-25 18:27:55 +00003728 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003729 OpInfo.ConstraintVT);
Chris Lattner7cbeb242008-02-21 04:55:52 +00003730 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003731 return;
Chris Lattnerbf996f12007-04-30 17:29:31 +00003732 }
3733 }
3734
3735 // Otherwise, we couldn't allocate enough registers for this.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003736 return;
Chris Lattnerbf996f12007-04-30 17:29:31 +00003737}
3738
3739
Chris Lattnerce7518c2006-01-26 22:24:51 +00003740/// visitInlineAsm - Handle a call to an InlineAsm object.
3741///
Duncan Sandsfd7b3262007-12-17 18:08:19 +00003742void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
3743 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Chris Lattnerce7518c2006-01-26 22:24:51 +00003744
Chris Lattner0c583402007-04-28 20:49:53 +00003745 /// ConstraintOperands - Information about all of the constraints.
Evan Cheng5c807602008-02-26 02:33:44 +00003746 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Chris Lattnerce7518c2006-01-26 22:24:51 +00003747
3748 SDOperand Chain = getRoot();
3749 SDOperand Flag;
3750
Chris Lattner4e4b5762006-02-01 18:59:47 +00003751 std::set<unsigned> OutputRegs, InputRegs;
Chris Lattner1efa40f2006-02-22 00:56:39 +00003752
Chris Lattner0c583402007-04-28 20:49:53 +00003753 // Do a prepass over the constraints, canonicalizing them, and building up the
3754 // ConstraintOperands list.
3755 std::vector<InlineAsm::ConstraintInfo>
3756 ConstraintInfos = IA->ParseConstraints();
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003757
3758 // SawEarlyClobber - Keep track of whether we saw an earlyclobber output
3759 // constraint. If so, we can't let the register allocator allocate any input
3760 // registers, because it will not know to avoid the earlyclobbered output reg.
3761 bool SawEarlyClobber = false;
3762
Duncan Sandsfd7b3262007-12-17 18:08:19 +00003763 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
Chris Lattneracf8b012008-04-27 23:44:28 +00003764 unsigned ResNo = 0; // ResNo - The result number of the next output.
Chris Lattner0c583402007-04-28 20:49:53 +00003765 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
Evan Cheng5c807602008-02-26 02:33:44 +00003766 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
3767 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Chris Lattner0c583402007-04-28 20:49:53 +00003768
Chris Lattner0c583402007-04-28 20:49:53 +00003769 MVT::ValueType OpVT = MVT::Other;
3770
3771 // Compute the value type for each operand.
3772 switch (OpInfo.Type) {
Chris Lattner1efa40f2006-02-22 00:56:39 +00003773 case InlineAsm::isOutput:
Chris Lattneracf8b012008-04-27 23:44:28 +00003774 // Indirect outputs just consume an argument.
3775 if (OpInfo.isIndirect) {
Duncan Sandsfd7b3262007-12-17 18:08:19 +00003776 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
Chris Lattneracf8b012008-04-27 23:44:28 +00003777 break;
Chris Lattner1efa40f2006-02-22 00:56:39 +00003778 }
Chris Lattneracf8b012008-04-27 23:44:28 +00003779 // The return value of the call is this value. As such, there is no
3780 // corresponding argument.
3781 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
3782 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
3783 OpVT = TLI.getValueType(STy->getElementType(ResNo));
3784 } else {
3785 assert(ResNo == 0 && "Asm only has one result!");
3786 OpVT = TLI.getValueType(CS.getType());
3787 }
3788 ++ResNo;
Chris Lattner1efa40f2006-02-22 00:56:39 +00003789 break;
3790 case InlineAsm::isInput:
Duncan Sandsfd7b3262007-12-17 18:08:19 +00003791 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
Chris Lattner1efa40f2006-02-22 00:56:39 +00003792 break;
3793 case InlineAsm::isClobber:
Chris Lattner0c583402007-04-28 20:49:53 +00003794 // Nothing to do.
Chris Lattner1efa40f2006-02-22 00:56:39 +00003795 break;
3796 }
Chris Lattner1efa40f2006-02-22 00:56:39 +00003797
Chris Lattner0c583402007-04-28 20:49:53 +00003798 // If this is an input or an indirect output, process the call argument.
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003799 // BasicBlocks are labels, currently appearing only in asm's.
Chris Lattner0c583402007-04-28 20:49:53 +00003800 if (OpInfo.CallOperandVal) {
Chris Lattner507ffd22008-04-27 00:16:18 +00003801 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal))
3802 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003803 else {
3804 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
3805 const Type *OpTy = OpInfo.CallOperandVal->getType();
3806 // If this is an indirect operand, the operand is a pointer to the
3807 // accessed type.
3808 if (OpInfo.isIndirect)
3809 OpTy = cast<PointerType>(OpTy)->getElementType();
3810
3811 // If OpTy is not a first-class value, it may be a struct/union that we
3812 // can tile with integers.
3813 if (!OpTy->isFirstClassType() && OpTy->isSized()) {
3814 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
3815 switch (BitSize) {
3816 default: break;
3817 case 1:
3818 case 8:
3819 case 16:
3820 case 32:
3821 case 64:
3822 OpTy = IntegerType::get(BitSize);
3823 break;
3824 }
Chris Lattner6995cf62007-04-29 18:58:03 +00003825 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003826
3827 OpVT = TLI.getValueType(OpTy, true);
Chris Lattner0c583402007-04-28 20:49:53 +00003828 }
3829 }
3830
3831 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a600be2007-04-28 21:01:43 +00003832
Chris Lattner3ff90dc2007-04-30 17:16:27 +00003833 // Compute the constraint code and ConstraintType to use.
Chris Lattner5a096902008-04-27 00:37:18 +00003834 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Chris Lattner0c583402007-04-28 20:49:53 +00003835
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003836 // Keep track of whether we see an earlyclobber.
3837 SawEarlyClobber |= OpInfo.isEarlyClobber;
Chris Lattner09e4b7e2007-04-28 21:12:06 +00003838
Chris Lattner0fe71e92008-02-21 19:43:13 +00003839 // If we see a clobber of a register, it is an early clobber.
Chris Lattner69e6a8d2008-02-21 20:54:31 +00003840 if (!SawEarlyClobber &&
3841 OpInfo.Type == InlineAsm::isClobber &&
3842 OpInfo.ConstraintType == TargetLowering::C_Register) {
3843 // Note that we want to ignore things that we don't trick here, like
3844 // dirflag, fpsr, flags, etc.
3845 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
3846 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
3847 OpInfo.ConstraintVT);
3848 if (PhysReg.first || PhysReg.second) {
3849 // This is a register we know of.
3850 SawEarlyClobber = true;
3851 }
3852 }
Chris Lattner0fe71e92008-02-21 19:43:13 +00003853
Chris Lattner09e4b7e2007-04-28 21:12:06 +00003854 // If this is a memory input, and if the operand is not indirect, do what we
3855 // need to to provide an address for the memory input.
3856 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
3857 !OpInfo.isIndirect) {
3858 assert(OpInfo.Type == InlineAsm::isInput &&
3859 "Can only indirectify direct input operands!");
3860
3861 // Memory operands really want the address of the value. If we don't have
3862 // an indirect input, put it in the constpool if we can, otherwise spill
3863 // it to a stack slot.
3864
3865 // If the operand is a float, integer, or vector constant, spill to a
3866 // constant pool entry to get its address.
3867 Value *OpVal = OpInfo.CallOperandVal;
3868 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
3869 isa<ConstantVector>(OpVal)) {
3870 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
3871 TLI.getPointerTy());
3872 } else {
3873 // Otherwise, create a stack slot and emit a store to it before the
3874 // asm.
3875 const Type *Ty = OpVal->getType();
Duncan Sands514ab342007-11-01 20:53:16 +00003876 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattner09e4b7e2007-04-28 21:12:06 +00003877 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3878 MachineFunction &MF = DAG.getMachineFunction();
3879 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
3880 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3881 Chain = DAG.getStore(Chain, OpInfo.CallOperand, StackSlot, NULL, 0);
3882 OpInfo.CallOperand = StackSlot;
3883 }
3884
3885 // There is no longer a Value* corresponding to this operand.
3886 OpInfo.CallOperandVal = 0;
3887 // It is now an indirect operand.
3888 OpInfo.isIndirect = true;
3889 }
3890
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003891 // If this constraint is for a specific register, allocate it before
3892 // anything else.
3893 if (OpInfo.ConstraintType == TargetLowering::C_Register)
3894 GetRegistersForValue(OpInfo, SawEarlyClobber, OutputRegs, InputRegs);
Chris Lattner0c583402007-04-28 20:49:53 +00003895 }
Chris Lattner0c583402007-04-28 20:49:53 +00003896 ConstraintInfos.clear();
3897
3898
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003899 // Second pass - Loop over all of the operands, assigning virtual or physregs
3900 // to registerclass operands.
3901 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Evan Cheng5c807602008-02-26 02:33:44 +00003902 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003903
3904 // C_Register operands have already been allocated, Other/Memory don't need
3905 // to be.
3906 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
3907 GetRegistersForValue(OpInfo, SawEarlyClobber, OutputRegs, InputRegs);
3908 }
3909
Chris Lattner0c583402007-04-28 20:49:53 +00003910 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
3911 std::vector<SDOperand> AsmNodeOperands;
3912 AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain
3913 AsmNodeOperands.push_back(
3914 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
3915
Chris Lattner2cc2f662006-02-01 01:28:23 +00003916
Chris Lattner0f0b7d42006-02-21 23:12:12 +00003917 // Loop over all of the inputs, copying the operand values into the
3918 // appropriate registers and processing the output regs.
Chris Lattner864635a2006-02-22 22:37:12 +00003919 RegsForValue RetValRegs;
Chris Lattner0f0b7d42006-02-21 23:12:12 +00003920
Chris Lattner0c583402007-04-28 20:49:53 +00003921 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
3922 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
3923
3924 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Evan Cheng5c807602008-02-26 02:33:44 +00003925 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Chris Lattner1efa40f2006-02-22 00:56:39 +00003926
Chris Lattner0c583402007-04-28 20:49:53 +00003927 switch (OpInfo.Type) {
Chris Lattner2cc2f662006-02-01 01:28:23 +00003928 case InlineAsm::isOutput: {
Chris Lattnerc83994e2007-04-28 21:03:16 +00003929 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
3930 OpInfo.ConstraintType != TargetLowering::C_Register) {
Chris Lattnerf2f3cd52007-04-28 06:08:13 +00003931 // Memory output, or 'other' output (e.g. 'X' constraint).
Chris Lattner09e4b7e2007-04-28 21:12:06 +00003932 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
Chris Lattner22873462006-02-27 23:45:39 +00003933
Chris Lattner22873462006-02-27 23:45:39 +00003934 // Add information to the INLINEASM node to know about this output.
3935 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
Chris Lattnerc90233b2007-05-15 01:33:58 +00003936 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
3937 TLI.getPointerTy()));
Chris Lattner09e4b7e2007-04-28 21:12:06 +00003938 AsmNodeOperands.push_back(OpInfo.CallOperand);
Chris Lattner22873462006-02-27 23:45:39 +00003939 break;
3940 }
3941
Chris Lattner2a600be2007-04-28 21:01:43 +00003942 // Otherwise, this is a register or register class output.
Chris Lattner22873462006-02-27 23:45:39 +00003943
Chris Lattner864635a2006-02-22 22:37:12 +00003944 // Copy the output from the appropriate register. Find a register that
Chris Lattner1efa40f2006-02-22 00:56:39 +00003945 // we can use.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003946 if (OpInfo.AssignedRegs.Regs.empty()) {
Bill Wendling832171c2006-12-07 20:04:42 +00003947 cerr << "Couldn't allocate output reg for contraint '"
Chris Lattner0c583402007-04-28 20:49:53 +00003948 << OpInfo.ConstraintCode << "'!\n";
Chris Lattnerd03f1582006-10-31 07:33:13 +00003949 exit(1);
3950 }
Chris Lattner1efa40f2006-02-22 00:56:39 +00003951
Chris Lattner0c583402007-04-28 20:49:53 +00003952 if (!OpInfo.isIndirect) {
3953 // This is the result value of the call.
Chris Lattner864635a2006-02-22 22:37:12 +00003954 assert(RetValRegs.Regs.empty() &&
Chris Lattner2cc2f662006-02-01 01:28:23 +00003955 "Cannot have multiple output constraints yet!");
Duncan Sandsfd7b3262007-12-17 18:08:19 +00003956 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003957 RetValRegs = OpInfo.AssignedRegs;
Chris Lattner2cc2f662006-02-01 01:28:23 +00003958 } else {
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003959 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
Chris Lattner0c583402007-04-28 20:49:53 +00003960 OpInfo.CallOperandVal));
Chris Lattner2cc2f662006-02-01 01:28:23 +00003961 }
Chris Lattner6656dd12006-01-31 02:03:41 +00003962
3963 // Add information to the INLINEASM node to know that this register is
3964 // set.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00003965 OpInfo.AssignedRegs.AddInlineAsmOperands(2 /*REGDEF*/, DAG,
3966 AsmNodeOperands);
Chris Lattner6656dd12006-01-31 02:03:41 +00003967 break;
3968 }
3969 case InlineAsm::isInput: {
Chris Lattner0c583402007-04-28 20:49:53 +00003970 SDOperand InOperandVal = OpInfo.CallOperand;
Chris Lattner3d81fee2006-02-04 02:16:44 +00003971
Chris Lattner0c583402007-04-28 20:49:53 +00003972 if (isdigit(OpInfo.ConstraintCode[0])) { // Matching constraint?
Chris Lattner2223aea2006-02-02 00:25:23 +00003973 // If this is required to match an output register we have already set,
3974 // just use its register.
Chris Lattner0c583402007-04-28 20:49:53 +00003975 unsigned OperandNo = atoi(OpInfo.ConstraintCode.c_str());
Chris Lattner3d81fee2006-02-04 02:16:44 +00003976
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003977 // Scan until we find the definition we already emitted of this operand.
3978 // When we find it, create a RegsForValue operand.
3979 unsigned CurOp = 2; // The first operand.
3980 for (; OperandNo; --OperandNo) {
3981 // Advance to the next operand.
3982 unsigned NumOps =
3983 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
Chris Lattnera15cf702006-07-20 19:02:21 +00003984 assert(((NumOps & 7) == 2 /*REGDEF*/ ||
3985 (NumOps & 7) == 4 /*MEM*/) &&
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00003986 "Skipped past definitions?");
3987 CurOp += (NumOps>>3)+1;
3988 }
3989
3990 unsigned NumOps =
3991 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
Chris Lattner527fae12007-02-01 01:21:12 +00003992 if ((NumOps & 7) == 2 /*REGDEF*/) {
3993 // Add NumOps>>3 registers to MatchedRegs.
3994 RegsForValue MatchedRegs;
Dan Gohman23ce5022008-04-25 18:27:55 +00003995 MatchedRegs.TLI = &TLI;
3996 MatchedRegs.ValueVTs.resize(1, InOperandVal.getValueType());
3997 MatchedRegs.RegVTs.resize(1, AsmNodeOperands[CurOp+1].getValueType());
Chris Lattner527fae12007-02-01 01:21:12 +00003998 for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
3999 unsigned Reg =
4000 cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
4001 MatchedRegs.Regs.push_back(Reg);
4002 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00004003
Chris Lattner527fae12007-02-01 01:21:12 +00004004 // Use the produced MatchedRegs object to
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004005 MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
Chris Lattner527fae12007-02-01 01:21:12 +00004006 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
4007 break;
4008 } else {
4009 assert((NumOps & 7) == 4/*MEM*/ && "Unknown matching constraint!");
Chris Lattnerf9853bc2008-02-21 05:27:19 +00004010 assert((NumOps >> 3) == 1 && "Unexpected number of operands");
4011 // Add information to the INLINEASM node to know about this input.
4012 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
4013 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
4014 TLI.getPointerTy()));
4015 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
4016 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00004017 }
Chris Lattner2223aea2006-02-02 00:25:23 +00004018 }
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004019
Chris Lattner2a600be2007-04-28 21:01:43 +00004020 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Chris Lattner0c583402007-04-28 20:49:53 +00004021 assert(!OpInfo.isIndirect &&
Chris Lattner44b2c502007-04-28 06:42:38 +00004022 "Don't know how to handle indirect other inputs yet!");
4023
Chris Lattner48884cd2007-08-25 00:47:38 +00004024 std::vector<SDOperand> Ops;
4025 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
4026 Ops, DAG);
4027 if (Ops.empty()) {
Bill Wendling832171c2006-12-07 20:04:42 +00004028 cerr << "Invalid operand for inline asm constraint '"
Chris Lattner0c583402007-04-28 20:49:53 +00004029 << OpInfo.ConstraintCode << "'!\n";
Chris Lattner53069fb2006-10-31 19:41:18 +00004030 exit(1);
4031 }
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004032
4033 // Add information to the INLINEASM node to know about this input.
Chris Lattner48884cd2007-08-25 00:47:38 +00004034 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Chris Lattnerc90233b2007-05-15 01:33:58 +00004035 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
4036 TLI.getPointerTy()));
Chris Lattner48884cd2007-08-25 00:47:38 +00004037 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004038 break;
Chris Lattner2a600be2007-04-28 21:01:43 +00004039 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Chris Lattner09e4b7e2007-04-28 21:12:06 +00004040 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Chris Lattner44b2c502007-04-28 06:42:38 +00004041 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
4042 "Memory operands expect pointer values");
4043
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004044 // Add information to the INLINEASM node to know about this input.
4045 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
Chris Lattnerc90233b2007-05-15 01:33:58 +00004046 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
4047 TLI.getPointerTy()));
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004048 AsmNodeOperands.push_back(InOperandVal);
4049 break;
4050 }
4051
Chris Lattner2a600be2007-04-28 21:01:43 +00004052 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
4053 OpInfo.ConstraintType == TargetLowering::C_Register) &&
4054 "Unknown constraint type!");
Chris Lattner0c583402007-04-28 20:49:53 +00004055 assert(!OpInfo.isIndirect &&
Chris Lattner44b2c502007-04-28 06:42:38 +00004056 "Don't know how to handle indirect register inputs yet!");
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004057
4058 // Copy the input into the appropriate registers.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00004059 assert(!OpInfo.AssignedRegs.Regs.empty() &&
4060 "Couldn't allocate input reg!");
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004061
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004062 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
Chris Lattner87bc3bd2006-02-24 01:11:24 +00004063
Chris Lattnere7cf56a2007-04-30 21:11:17 +00004064 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG,
4065 AsmNodeOperands);
Chris Lattner6656dd12006-01-31 02:03:41 +00004066 break;
4067 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00004068 case InlineAsm::isClobber: {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00004069 // Add the clobbered value to the operand list, so that the register
4070 // allocator is aware that the physreg got clobbered.
Chris Lattnere7cf56a2007-04-30 21:11:17 +00004071 if (!OpInfo.AssignedRegs.Regs.empty())
4072 OpInfo.AssignedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG,
4073 AsmNodeOperands);
Chris Lattner6656dd12006-01-31 02:03:41 +00004074 break;
4075 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00004076 }
Chris Lattner6656dd12006-01-31 02:03:41 +00004077 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00004078
4079 // Finish up input operands.
4080 AsmNodeOperands[0] = Chain;
4081 if (Flag.Val) AsmNodeOperands.push_back(Flag);
4082
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004083 Chain = DAG.getNode(ISD::INLINEASM,
4084 DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004085 &AsmNodeOperands[0], AsmNodeOperands.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00004086 Flag = Chain.getValue(1);
4087
Chris Lattner6656dd12006-01-31 02:03:41 +00004088 // If this asm returns a register value, copy the result from that register
4089 // and set it as the value of the call.
Chris Lattner3a508c92007-04-12 06:00:20 +00004090 if (!RetValRegs.Regs.empty()) {
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004091 SDOperand Val = RetValRegs.getCopyFromRegs(DAG, Chain, &Flag);
Chris Lattner3a508c92007-04-12 06:00:20 +00004092
4093 // If the result of the inline asm is a vector, it may have the wrong
4094 // width/num elts. Make sure to convert it to the right type with
Dan Gohman7f321562007-06-25 16:23:39 +00004095 // bit_convert.
4096 if (MVT::isVector(Val.getValueType())) {
Duncan Sandsfd7b3262007-12-17 18:08:19 +00004097 const VectorType *VTy = cast<VectorType>(CS.getType());
Dan Gohman7f321562007-06-25 16:23:39 +00004098 MVT::ValueType DesiredVT = TLI.getValueType(VTy);
Chris Lattner3a508c92007-04-12 06:00:20 +00004099
Dan Gohman7f321562007-06-25 16:23:39 +00004100 Val = DAG.getNode(ISD::BIT_CONVERT, DesiredVT, Val);
Chris Lattner3a508c92007-04-12 06:00:20 +00004101 }
4102
Duncan Sandsfd7b3262007-12-17 18:08:19 +00004103 setValue(CS.getInstruction(), Val);
Chris Lattner3a508c92007-04-12 06:00:20 +00004104 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00004105
Chris Lattner6656dd12006-01-31 02:03:41 +00004106 std::vector<std::pair<SDOperand, Value*> > StoresToEmit;
4107
4108 // Process indirect outputs, first output all of the flagged copies out of
4109 // physregs.
4110 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
Chris Lattner864635a2006-02-22 22:37:12 +00004111 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Chris Lattner6656dd12006-01-31 02:03:41 +00004112 Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004113 SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, &Flag);
Chris Lattner864635a2006-02-22 22:37:12 +00004114 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6656dd12006-01-31 02:03:41 +00004115 }
4116
4117 // Emit the non-flagged stores from the physregs.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004118 SmallVector<SDOperand, 8> OutChains;
Chris Lattner6656dd12006-01-31 02:03:41 +00004119 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Chris Lattner0c583402007-04-28 20:49:53 +00004120 OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first,
Chris Lattner6656dd12006-01-31 02:03:41 +00004121 getValue(StoresToEmit[i].second),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004122 StoresToEmit[i].second, 0));
Chris Lattner6656dd12006-01-31 02:03:41 +00004123 if (!OutChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004124 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4125 &OutChains[0], OutChains.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00004126 DAG.setRoot(Chain);
4127}
4128
4129
Chris Lattner1c08c712005-01-07 07:47:53 +00004130void SelectionDAGLowering::visitMalloc(MallocInst &I) {
4131 SDOperand Src = getValue(I.getOperand(0));
4132
4133 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattner68cd65e2005-01-22 23:04:37 +00004134
4135 if (IntPtr < Src.getValueType())
4136 Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
4137 else if (IntPtr > Src.getValueType())
4138 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
Chris Lattner1c08c712005-01-07 07:47:53 +00004139
4140 // Scale the source by the type size.
Duncan Sands514ab342007-11-01 20:53:16 +00004141 uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType());
Chris Lattner1c08c712005-01-07 07:47:53 +00004142 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
Chris Lattner0bd48932008-01-17 07:00:52 +00004143 Src, DAG.getIntPtrConstant(ElementSize));
Chris Lattner1c08c712005-01-07 07:47:53 +00004144
Reid Spencer47857812006-12-31 05:55:36 +00004145 TargetLowering::ArgListTy Args;
4146 TargetLowering::ArgListEntry Entry;
4147 Entry.Node = Src;
4148 Entry.Ty = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004149 Args.push_back(Entry);
Chris Lattnercf5734d2005-01-08 19:26:18 +00004150
4151 std::pair<SDOperand,SDOperand> Result =
Duncan Sands00fee652008-02-14 17:28:50 +00004152 TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, CallingConv::C,
4153 true, DAG.getExternalSymbol("malloc", IntPtr), Args, DAG);
Chris Lattnercf5734d2005-01-08 19:26:18 +00004154 setValue(&I, Result.first); // Pointers always fit in registers
4155 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +00004156}
4157
4158void SelectionDAGLowering::visitFree(FreeInst &I) {
Reid Spencer47857812006-12-31 05:55:36 +00004159 TargetLowering::ArgListTy Args;
4160 TargetLowering::ArgListEntry Entry;
4161 Entry.Node = getValue(I.getOperand(0));
4162 Entry.Ty = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004163 Args.push_back(Entry);
Chris Lattner1c08c712005-01-07 07:47:53 +00004164 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattnercf5734d2005-01-08 19:26:18 +00004165 std::pair<SDOperand,SDOperand> Result =
Duncan Sands00fee652008-02-14 17:28:50 +00004166 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false,
4167 CallingConv::C, true,
Chris Lattnercf5734d2005-01-08 19:26:18 +00004168 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
4169 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +00004170}
4171
Evan Chengff9b3732008-01-30 18:18:23 +00004172// EmitInstrWithCustomInserter - This method should be implemented by targets
4173// that mark instructions with the 'usesCustomDAGSchedInserter' flag. These
Chris Lattner025c39b2005-08-26 20:54:47 +00004174// instructions are special in various ways, which require special support to
4175// insert. The specified MachineInstr is created but not inserted into any
4176// basic blocks, and the scheduler passes ownership of it to this method.
Evan Chengff9b3732008-01-30 18:18:23 +00004177MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Chris Lattner025c39b2005-08-26 20:54:47 +00004178 MachineBasicBlock *MBB) {
Bill Wendling832171c2006-12-07 20:04:42 +00004179 cerr << "If a target marks an instruction with "
4180 << "'usesCustomDAGSchedInserter', it must implement "
Evan Chengff9b3732008-01-30 18:18:23 +00004181 << "TargetLowering::EmitInstrWithCustomInserter!\n";
Chris Lattner025c39b2005-08-26 20:54:47 +00004182 abort();
4183 return 0;
4184}
4185
Chris Lattner39ae3622005-01-09 00:00:49 +00004186void SelectionDAGLowering::visitVAStart(CallInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00004187 DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(),
4188 getValue(I.getOperand(1)),
4189 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner39ae3622005-01-09 00:00:49 +00004190}
4191
4192void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00004193 SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
4194 getValue(I.getOperand(0)),
4195 DAG.getSrcValue(I.getOperand(0)));
4196 setValue(&I, V);
4197 DAG.setRoot(V.getValue(1));
Chris Lattner1c08c712005-01-07 07:47:53 +00004198}
4199
4200void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00004201 DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
4202 getValue(I.getOperand(1)),
4203 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner1c08c712005-01-07 07:47:53 +00004204}
4205
4206void SelectionDAGLowering::visitVACopy(CallInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00004207 DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(),
4208 getValue(I.getOperand(1)),
4209 getValue(I.getOperand(2)),
4210 DAG.getSrcValue(I.getOperand(1)),
4211 DAG.getSrcValue(I.getOperand(2))));
Chris Lattner1c08c712005-01-07 07:47:53 +00004212}
4213
Chris Lattnerfdfded52006-04-12 16:20:43 +00004214/// TargetLowering::LowerArguments - This is the default LowerArguments
4215/// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004216/// targets are migrated to using FORMAL_ARGUMENTS, this hook should be
4217/// integrated into SDISel.
Chris Lattnerfdfded52006-04-12 16:20:43 +00004218std::vector<SDOperand>
4219TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
4220 // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
4221 std::vector<SDOperand> Ops;
Chris Lattner8c0c10c2006-05-16 06:45:34 +00004222 Ops.push_back(DAG.getRoot());
Chris Lattnerfdfded52006-04-12 16:20:43 +00004223 Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy()));
4224 Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy()));
4225
4226 // Add one result value for each formal argument.
4227 std::vector<MVT::ValueType> RetVals;
Anton Korobeynikov6aa279d2007-01-28 18:01:49 +00004228 unsigned j = 1;
Anton Korobeynikovac2b2cf2007-01-28 16:04:40 +00004229 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
4230 I != E; ++I, ++j) {
Chris Lattnerfdfded52006-04-12 16:20:43 +00004231 MVT::ValueType VT = getValueType(I->getType());
Duncan Sands276dcbd2008-03-21 09:14:45 +00004232 ISD::ArgFlagsTy Flags;
Lauro Ramos Venancio7aa47b62007-02-13 13:50:08 +00004233 unsigned OriginalAlignment =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004234 getTargetData()->getABITypeAlignment(I->getType());
Lauro Ramos Venancio7aa47b62007-02-13 13:50:08 +00004235
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00004236 if (F.paramHasAttr(j, ParamAttr::ZExt))
Duncan Sands276dcbd2008-03-21 09:14:45 +00004237 Flags.setZExt();
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00004238 if (F.paramHasAttr(j, ParamAttr::SExt))
Duncan Sands276dcbd2008-03-21 09:14:45 +00004239 Flags.setSExt();
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00004240 if (F.paramHasAttr(j, ParamAttr::InReg))
Duncan Sands276dcbd2008-03-21 09:14:45 +00004241 Flags.setInReg();
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00004242 if (F.paramHasAttr(j, ParamAttr::StructRet))
Duncan Sands276dcbd2008-03-21 09:14:45 +00004243 Flags.setSRet();
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00004244 if (F.paramHasAttr(j, ParamAttr::ByVal)) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00004245 Flags.setByVal();
Rafael Espindola594d37e2007-08-10 14:44:42 +00004246 const PointerType *Ty = cast<PointerType>(I->getType());
Duncan Sandsa41d7192008-01-13 21:19:59 +00004247 const Type *ElementTy = Ty->getElementType();
Duncan Sands276dcbd2008-03-21 09:14:45 +00004248 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sandsa41d7192008-01-13 21:19:59 +00004249 unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy);
Dale Johannesen08e78b12008-02-22 17:49:45 +00004250 // For ByVal, alignment should be passed from FE. BE will guess if
4251 // this info is not there but there are cases it cannot get right.
4252 if (F.getParamAlignment(j))
Duncan Sands276dcbd2008-03-21 09:14:45 +00004253 FrameAlign = F.getParamAlignment(j);
4254 Flags.setByValAlign(FrameAlign);
4255 Flags.setByValSize(FrameSize);
Rafael Espindola594d37e2007-08-10 14:44:42 +00004256 }
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00004257 if (F.paramHasAttr(j, ParamAttr::Nest))
Duncan Sands276dcbd2008-03-21 09:14:45 +00004258 Flags.setNest();
4259 Flags.setOrigAlign(OriginalAlignment);
Duncan Sandsb988bac2008-02-11 20:58:28 +00004260
4261 MVT::ValueType RegisterVT = getRegisterType(VT);
4262 unsigned NumRegs = getNumRegisters(VT);
4263 for (unsigned i = 0; i != NumRegs; ++i) {
4264 RetVals.push_back(RegisterVT);
Nicolas Geoffray9701c8a2008-04-14 17:17:14 +00004265 ISD::ArgFlagsTy MyFlags = Flags;
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00004266 if (NumRegs > 1 && i == 0)
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00004267 MyFlags.setSplit();
Duncan Sandsb988bac2008-02-11 20:58:28 +00004268 // if it isn't first piece, alignment must be 1
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00004269 else if (i > 0)
Nicolas Geoffray9701c8a2008-04-14 17:17:14 +00004270 MyFlags.setOrigAlign(1);
4271 Ops.push_back(DAG.getArgFlags(MyFlags));
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004272 }
Chris Lattnerfdfded52006-04-12 16:20:43 +00004273 }
Evan Cheng3b0d2862006-04-25 23:03:35 +00004274
Chris Lattner8c0c10c2006-05-16 06:45:34 +00004275 RetVals.push_back(MVT::Other);
Chris Lattnerfdfded52006-04-12 16:20:43 +00004276
4277 // Create the node.
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004278 SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS,
Chris Lattner86ca3ca2008-02-13 07:39:09 +00004279 DAG.getVTList(&RetVals[0], RetVals.size()),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004280 &Ops[0], Ops.size()).Val;
Chris Lattner86ca3ca2008-02-13 07:39:09 +00004281
4282 // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but
4283 // allows exposing the loads that may be part of the argument access to the
4284 // first DAGCombiner pass.
4285 SDOperand TmpRes = LowerOperation(SDOperand(Result, 0), DAG);
4286
4287 // The number of results should match up, except that the lowered one may have
4288 // an extra flag result.
4289 assert((Result->getNumValues() == TmpRes.Val->getNumValues() ||
4290 (Result->getNumValues()+1 == TmpRes.Val->getNumValues() &&
4291 TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag))
4292 && "Lowering produced unexpected number of results!");
4293 Result = TmpRes.Val;
4294
Dan Gohman27a70be2007-07-02 16:18:06 +00004295 unsigned NumArgRegs = Result->getNumValues() - 1;
4296 DAG.setRoot(SDOperand(Result, NumArgRegs));
Chris Lattnerfdfded52006-04-12 16:20:43 +00004297
4298 // Set up the return result vector.
4299 Ops.clear();
4300 unsigned i = 0;
Reid Spencer47857812006-12-31 05:55:36 +00004301 unsigned Idx = 1;
4302 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
4303 ++I, ++Idx) {
Chris Lattnerfdfded52006-04-12 16:20:43 +00004304 MVT::ValueType VT = getValueType(I->getType());
Duncan Sandsb988bac2008-02-11 20:58:28 +00004305 MVT::ValueType PartVT = getRegisterType(VT);
4306
4307 unsigned NumParts = getNumRegisters(VT);
4308 SmallVector<SDOperand, 4> Parts(NumParts);
4309 for (unsigned j = 0; j != NumParts; ++j)
4310 Parts[j] = SDOperand(Result, i++);
4311
4312 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4313 if (F.paramHasAttr(Idx, ParamAttr::SExt))
4314 AssertOp = ISD::AssertSext;
4315 else if (F.paramHasAttr(Idx, ParamAttr::ZExt))
4316 AssertOp = ISD::AssertZext;
4317
4318 Ops.push_back(getCopyFromParts(DAG, &Parts[0], NumParts, PartVT, VT,
Chris Lattner4468c1f2008-03-09 09:38:46 +00004319 AssertOp));
Chris Lattnerfdfded52006-04-12 16:20:43 +00004320 }
Dan Gohman27a70be2007-07-02 16:18:06 +00004321 assert(i == NumArgRegs && "Argument register count mismatch!");
Chris Lattnerfdfded52006-04-12 16:20:43 +00004322 return Ops;
4323}
4324
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004325
4326/// TargetLowering::LowerCallTo - This is the default LowerCallTo
4327/// implementation, which just inserts an ISD::CALL node, which is later custom
4328/// lowered by the target to something concrete. FIXME: When all targets are
4329/// migrated to using ISD::CALL, this hook should be integrated into SDISel.
4330std::pair<SDOperand, SDOperand>
Duncan Sands00fee652008-02-14 17:28:50 +00004331TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
4332 bool RetSExt, bool RetZExt, bool isVarArg,
4333 unsigned CallingConv, bool isTailCall,
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004334 SDOperand Callee,
4335 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004336 SmallVector<SDOperand, 32> Ops;
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004337 Ops.push_back(Chain); // Op#0 - Chain
4338 Ops.push_back(DAG.getConstant(CallingConv, getPointerTy())); // Op#1 - CC
4339 Ops.push_back(DAG.getConstant(isVarArg, getPointerTy())); // Op#2 - VarArg
4340 Ops.push_back(DAG.getConstant(isTailCall, getPointerTy())); // Op#3 - Tail
4341 Ops.push_back(Callee);
4342
4343 // Handle all of the outgoing arguments.
4344 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Reid Spencer47857812006-12-31 05:55:36 +00004345 MVT::ValueType VT = getValueType(Args[i].Ty);
4346 SDOperand Op = Args[i].Node;
Duncan Sands276dcbd2008-03-21 09:14:45 +00004347 ISD::ArgFlagsTy Flags;
Lauro Ramos Venancio7aa47b62007-02-13 13:50:08 +00004348 unsigned OriginalAlignment =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004349 getTargetData()->getABITypeAlignment(Args[i].Ty);
Duncan Sands276dcbd2008-03-21 09:14:45 +00004350
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004351 if (Args[i].isZExt)
Duncan Sands276dcbd2008-03-21 09:14:45 +00004352 Flags.setZExt();
4353 if (Args[i].isSExt)
4354 Flags.setSExt();
Anton Korobeynikov0db79d82007-03-06 06:10:33 +00004355 if (Args[i].isInReg)
Duncan Sands276dcbd2008-03-21 09:14:45 +00004356 Flags.setInReg();
Anton Korobeynikov0db79d82007-03-06 06:10:33 +00004357 if (Args[i].isSRet)
Duncan Sands276dcbd2008-03-21 09:14:45 +00004358 Flags.setSRet();
Rafael Espindola21485be2007-08-20 15:18:24 +00004359 if (Args[i].isByVal) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00004360 Flags.setByVal();
Rafael Espindola21485be2007-08-20 15:18:24 +00004361 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
Duncan Sandsa41d7192008-01-13 21:19:59 +00004362 const Type *ElementTy = Ty->getElementType();
Duncan Sands276dcbd2008-03-21 09:14:45 +00004363 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sandsa41d7192008-01-13 21:19:59 +00004364 unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy);
Dale Johannesen08e78b12008-02-22 17:49:45 +00004365 // For ByVal, alignment should come from FE. BE will guess if this
4366 // info is not there but there are cases it cannot get right.
4367 if (Args[i].Alignment)
Duncan Sands276dcbd2008-03-21 09:14:45 +00004368 FrameAlign = Args[i].Alignment;
4369 Flags.setByValAlign(FrameAlign);
4370 Flags.setByValSize(FrameSize);
Rafael Espindola21485be2007-08-20 15:18:24 +00004371 }
Duncan Sands36397f52007-07-27 12:58:54 +00004372 if (Args[i].isNest)
Duncan Sands276dcbd2008-03-21 09:14:45 +00004373 Flags.setNest();
4374 Flags.setOrigAlign(OriginalAlignment);
Dan Gohman27a70be2007-07-02 16:18:06 +00004375
Duncan Sandsb988bac2008-02-11 20:58:28 +00004376 MVT::ValueType PartVT = getRegisterType(VT);
4377 unsigned NumParts = getNumRegisters(VT);
4378 SmallVector<SDOperand, 4> Parts(NumParts);
4379 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
4380
4381 if (Args[i].isSExt)
4382 ExtendKind = ISD::SIGN_EXTEND;
4383 else if (Args[i].isZExt)
4384 ExtendKind = ISD::ZERO_EXTEND;
4385
4386 getCopyToParts(DAG, Op, &Parts[0], NumParts, PartVT, ExtendKind);
4387
4388 for (unsigned i = 0; i != NumParts; ++i) {
4389 // if it isn't first piece, alignment must be 1
Duncan Sands276dcbd2008-03-21 09:14:45 +00004390 ISD::ArgFlagsTy MyFlags = Flags;
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00004391 if (NumParts > 1 && i == 0)
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00004392 MyFlags.setSplit();
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00004393 else if (i != 0)
Duncan Sands276dcbd2008-03-21 09:14:45 +00004394 MyFlags.setOrigAlign(1);
Duncan Sandsb988bac2008-02-11 20:58:28 +00004395
4396 Ops.push_back(Parts[i]);
Duncan Sands276dcbd2008-03-21 09:14:45 +00004397 Ops.push_back(DAG.getArgFlags(MyFlags));
Dan Gohman27a70be2007-07-02 16:18:06 +00004398 }
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004399 }
4400
Dan Gohmanef5d1942008-03-11 21:11:25 +00004401 // Figure out the result value types. We start by making a list of
Dan Gohman23ce5022008-04-25 18:27:55 +00004402 // the potentially illegal return value types.
Dan Gohmanef5d1942008-03-11 21:11:25 +00004403 SmallVector<MVT::ValueType, 4> LoweredRetTys;
4404 SmallVector<MVT::ValueType, 4> RetTys;
Dan Gohman23ce5022008-04-25 18:27:55 +00004405 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanef5d1942008-03-11 21:11:25 +00004406
Dan Gohman23ce5022008-04-25 18:27:55 +00004407 // Then we translate that to a list of legal types.
4408 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4409 MVT::ValueType VT = RetTys[I];
Dan Gohmanef5d1942008-03-11 21:11:25 +00004410 MVT::ValueType RegisterVT = getRegisterType(VT);
4411 unsigned NumRegs = getNumRegisters(VT);
4412 for (unsigned i = 0; i != NumRegs; ++i)
4413 LoweredRetTys.push_back(RegisterVT);
4414 }
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004415
Dan Gohmanef5d1942008-03-11 21:11:25 +00004416 LoweredRetTys.push_back(MVT::Other); // Always has a chain.
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004417
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004418 // Create the CALL node.
Chris Lattnerbe384162006-08-16 22:57:46 +00004419 SDOperand Res = DAG.getNode(ISD::CALL,
Dan Gohmanef5d1942008-03-11 21:11:25 +00004420 DAG.getVTList(&LoweredRetTys[0],
4421 LoweredRetTys.size()),
Chris Lattnerbe384162006-08-16 22:57:46 +00004422 &Ops[0], Ops.size());
Dan Gohmanef5d1942008-03-11 21:11:25 +00004423 Chain = Res.getValue(LoweredRetTys.size() - 1);
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004424
4425 // Gather up the call result into a single value.
4426 if (RetTy != Type::VoidTy) {
Duncan Sands00fee652008-02-14 17:28:50 +00004427 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4428
4429 if (RetSExt)
4430 AssertOp = ISD::AssertSext;
4431 else if (RetZExt)
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004432 AssertOp = ISD::AssertZext;
Duncan Sands00fee652008-02-14 17:28:50 +00004433
Dan Gohmanef5d1942008-03-11 21:11:25 +00004434 SmallVector<SDOperand, 4> ReturnValues;
4435 unsigned RegNo = 0;
Dan Gohman23ce5022008-04-25 18:27:55 +00004436 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4437 MVT::ValueType VT = RetTys[I];
Dan Gohmanef5d1942008-03-11 21:11:25 +00004438 MVT::ValueType RegisterVT = getRegisterType(VT);
4439 unsigned NumRegs = getNumRegisters(VT);
4440 unsigned RegNoEnd = NumRegs + RegNo;
4441 SmallVector<SDOperand, 4> Results;
4442 for (; RegNo != RegNoEnd; ++RegNo)
4443 Results.push_back(Res.getValue(RegNo));
4444 SDOperand ReturnValue =
4445 getCopyFromParts(DAG, &Results[0], NumRegs, RegisterVT, VT,
4446 AssertOp);
4447 ReturnValues.push_back(ReturnValue);
4448 }
4449 Res = ReturnValues.size() == 1 ? ReturnValues.front() :
4450 DAG.getNode(ISD::MERGE_VALUES,
4451 DAG.getVTList(&RetTys[0], RetTys.size()),
4452 &ReturnValues[0], ReturnValues.size());
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004453 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004454
4455 return std::make_pair(Res, Chain);
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004456}
4457
Chris Lattner50381b62005-05-14 05:50:48 +00004458SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner171453a2005-01-16 07:28:41 +00004459 assert(0 && "LowerOperation not implemented for this target!");
4460 abort();
Misha Brukmand3f03e42005-02-17 21:39:27 +00004461 return SDOperand();
Chris Lattner171453a2005-01-16 07:28:41 +00004462}
4463
Nate Begeman0aed7842006-01-28 03:14:31 +00004464SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op,
4465 SelectionDAG &DAG) {
4466 assert(0 && "CustomPromoteOperation not implemented for this target!");
4467 abort();
4468 return SDOperand();
4469}
4470
Chris Lattner7041ee32005-01-11 05:56:49 +00004471//===----------------------------------------------------------------------===//
4472// SelectionDAGISel code
4473//===----------------------------------------------------------------------===//
Chris Lattner1c08c712005-01-07 07:47:53 +00004474
4475unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
Chris Lattner84bc5422007-12-31 04:13:23 +00004476 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
Chris Lattner1c08c712005-01-07 07:47:53 +00004477}
4478
Chris Lattner495a0b52005-08-17 06:37:43 +00004479void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004480 AU.addRequired<AliasAnalysis>();
Gordon Henriksence224772008-01-07 01:30:38 +00004481 AU.addRequired<CollectorModuleMetadata>();
Chris Lattnerc8d288f2007-03-31 04:18:03 +00004482 AU.setPreservesAll();
Chris Lattner495a0b52005-08-17 06:37:43 +00004483}
Chris Lattner1c08c712005-01-07 07:47:53 +00004484
Chris Lattnerc88d8e92005-12-05 07:10:48 +00004485
Chris Lattnerbad7f482006-10-28 19:22:10 +00004486
Chris Lattner1c08c712005-01-07 07:47:53 +00004487bool SelectionDAGISel::runOnFunction(Function &Fn) {
Dan Gohman5f43f922007-08-27 16:26:13 +00004488 // Get alias analysis for load/store combining.
4489 AA = &getAnalysis<AliasAnalysis>();
4490
Chris Lattner1c08c712005-01-07 07:47:53 +00004491 MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
Gordon Henriksence224772008-01-07 01:30:38 +00004492 if (MF.getFunction()->hasCollector())
4493 GCI = &getAnalysis<CollectorModuleMetadata>().get(*MF.getFunction());
4494 else
4495 GCI = 0;
Chris Lattner84bc5422007-12-31 04:13:23 +00004496 RegInfo = &MF.getRegInfo();
Bill Wendling832171c2006-12-07 20:04:42 +00004497 DOUT << "\n\n\n=== " << Fn.getName() << "\n";
Chris Lattner1c08c712005-01-07 07:47:53 +00004498
4499 FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
4500
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004501 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
4502 if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator()))
4503 // Mark landing pad.
4504 FuncInfo.MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Duncan Sands9fac0b52007-06-06 10:05:18 +00004505
4506 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
Chris Lattner1c08c712005-01-07 07:47:53 +00004507 SelectBasicBlock(I, MF, FuncInfo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004508
Evan Chengad2070c2007-02-10 02:43:39 +00004509 // Add function live-ins to entry block live-in set.
4510 BasicBlock *EntryBB = &Fn.getEntryBlock();
4511 BB = FuncInfo.MBBMap[EntryBB];
Chris Lattner84bc5422007-12-31 04:13:23 +00004512 if (!RegInfo->livein_empty())
4513 for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(),
4514 E = RegInfo->livein_end(); I != E; ++I)
Evan Chengad2070c2007-02-10 02:43:39 +00004515 BB->addLiveIn(I->first);
4516
Duncan Sandsf4070822007-06-15 19:04:19 +00004517#ifndef NDEBUG
4518 assert(FuncInfo.CatchInfoFound.size() == FuncInfo.CatchInfoLost.size() &&
4519 "Not all catch info was assigned to a landing pad!");
4520#endif
4521
Chris Lattner1c08c712005-01-07 07:47:53 +00004522 return true;
4523}
4524
Chris Lattner6833b062008-04-28 07:16:35 +00004525void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Chris Lattner571e4342006-10-27 21:36:01 +00004526 SDOperand Op = getValue(V);
Chris Lattner18c2f132005-01-13 20:50:02 +00004527 assert((Op.getOpcode() != ISD::CopyFromReg ||
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004528 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
Chris Lattner18c2f132005-01-13 20:50:02 +00004529 "Copy from a reg to the same reg!");
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004530 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
Dan Gohmanb6f5b002007-06-28 23:29:44 +00004531
Dan Gohman23ce5022008-04-25 18:27:55 +00004532 RegsForValue RFV(TLI, Reg, V->getType());
4533 SDOperand Chain = DAG.getEntryNode();
4534 RFV.getCopyToRegs(Op, DAG, Chain, 0);
4535 PendingExports.push_back(Chain);
Chris Lattner1c08c712005-01-07 07:47:53 +00004536}
4537
Chris Lattner068a81e2005-01-17 17:15:02 +00004538void SelectionDAGISel::
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004539LowerArguments(BasicBlock *LLVMBB, SelectionDAGLowering &SDL) {
Chris Lattner068a81e2005-01-17 17:15:02 +00004540 // If this is the entry block, emit arguments.
Evan Cheng15699fc2007-02-10 01:08:18 +00004541 Function &F = *LLVMBB->getParent();
Chris Lattner0afa8e32005-01-17 17:55:19 +00004542 FunctionLoweringInfo &FuncInfo = SDL.FuncInfo;
Chris Lattnerbf209482005-10-30 19:42:35 +00004543 SDOperand OldRoot = SDL.DAG.getRoot();
4544 std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
Chris Lattner068a81e2005-01-17 17:15:02 +00004545
Chris Lattnerbf209482005-10-30 19:42:35 +00004546 unsigned a = 0;
4547 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
4548 AI != E; ++AI, ++a)
4549 if (!AI->use_empty()) {
4550 SDL.setValue(AI, Args[a]);
Evan Chengf7179bb2006-04-27 08:29:42 +00004551
Chris Lattnerbf209482005-10-30 19:42:35 +00004552 // If this argument is live outside of the entry block, insert a copy from
4553 // whereever we got it to the vreg that other BB's will reference it as.
Chris Lattner251db182007-02-25 18:40:32 +00004554 DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo.ValueMap.find(AI);
4555 if (VMI != FuncInfo.ValueMap.end()) {
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004556 SDL.CopyValueToVirtualRegister(AI, VMI->second);
Chris Lattnerbf209482005-10-30 19:42:35 +00004557 }
Chris Lattner0afa8e32005-01-17 17:55:19 +00004558 }
Chris Lattnerbf209482005-10-30 19:42:35 +00004559
Chris Lattnerbf209482005-10-30 19:42:35 +00004560 // Finally, if the target has anything special to do, allow it to do so.
Chris Lattner96645412006-05-16 06:10:58 +00004561 // FIXME: this should insert code into the DAG!
Chris Lattnerbf209482005-10-30 19:42:35 +00004562 EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction());
Chris Lattner068a81e2005-01-17 17:15:02 +00004563}
4564
Duncan Sandsf4070822007-06-15 19:04:19 +00004565static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB,
4566 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
Duncan Sandsf4070822007-06-15 19:04:19 +00004567 for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I)
Duncan Sandscf26d7c2007-07-04 20:52:51 +00004568 if (isSelector(I)) {
Duncan Sandsf4070822007-06-15 19:04:19 +00004569 // Apply the catch info to DestBB.
4570 addCatchInfo(cast<CallInst>(*I), MMI, FLI.MBBMap[DestBB]);
4571#ifndef NDEBUG
Duncan Sands560a7372007-11-15 09:54:37 +00004572 if (!FLI.MBBMap[SrcBB]->isLandingPad())
4573 FLI.CatchInfoFound.insert(I);
Duncan Sandsf4070822007-06-15 19:04:19 +00004574#endif
4575 }
4576}
4577
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004578/// CheckDAGForTailCallsAndFixThem - This Function looks for CALL nodes in the
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00004579/// DAG and fixes their tailcall attribute operand.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004580static void CheckDAGForTailCallsAndFixThem(SelectionDAG &DAG,
4581 TargetLowering& TLI) {
4582 SDNode * Ret = NULL;
4583 SDOperand Terminator = DAG.getRoot();
4584
4585 // Find RET node.
4586 if (Terminator.getOpcode() == ISD::RET) {
4587 Ret = Terminator.Val;
4588 }
4589
4590 // Fix tail call attribute of CALL nodes.
4591 for (SelectionDAG::allnodes_iterator BE = DAG.allnodes_begin(),
4592 BI = prior(DAG.allnodes_end()); BI != BE; --BI) {
4593 if (BI->getOpcode() == ISD::CALL) {
4594 SDOperand OpRet(Ret, 0);
4595 SDOperand OpCall(static_cast<SDNode*>(BI), 0);
4596 bool isMarkedTailCall =
4597 cast<ConstantSDNode>(OpCall.getOperand(3))->getValue() != 0;
4598 // If CALL node has tail call attribute set to true and the call is not
4599 // eligible (no RET or the target rejects) the attribute is fixed to
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00004600 // false. The TargetLowering::IsEligibleForTailCallOptimization function
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004601 // must correctly identify tail call optimizable calls.
4602 if (isMarkedTailCall &&
4603 (Ret==NULL ||
4604 !TLI.IsEligibleForTailCallOptimization(OpCall, OpRet, DAG))) {
4605 SmallVector<SDOperand, 32> Ops;
4606 unsigned idx=0;
4607 for(SDNode::op_iterator I =OpCall.Val->op_begin(),
4608 E=OpCall.Val->op_end(); I!=E; I++, idx++) {
4609 if (idx!=3)
4610 Ops.push_back(*I);
4611 else
4612 Ops.push_back(DAG.getConstant(false, TLI.getPointerTy()));
4613 }
4614 DAG.UpdateNodeOperands(OpCall, Ops.begin(), Ops.size());
4615 }
4616 }
4617 }
4618}
4619
Chris Lattner1c08c712005-01-07 07:47:53 +00004620void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
4621 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
Nate Begemanf15485a2006-03-27 01:32:24 +00004622 FunctionLoweringInfo &FuncInfo) {
Gordon Henriksence224772008-01-07 01:30:38 +00004623 SelectionDAGLowering SDL(DAG, TLI, *AA, FuncInfo, GCI);
Chris Lattnerddb870b2005-01-13 17:59:43 +00004624
Chris Lattnerbf209482005-10-30 19:42:35 +00004625 // Lower any arguments needed in this block if this is the entry block.
Dan Gohmanecb7a772007-03-22 16:38:57 +00004626 if (LLVMBB == &LLVMBB->getParent()->getEntryBlock())
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004627 LowerArguments(LLVMBB, SDL);
Chris Lattner1c08c712005-01-07 07:47:53 +00004628
4629 BB = FuncInfo.MBBMap[LLVMBB];
4630 SDL.setCurrentBasicBlock(BB);
4631
Duncan Sandsf4070822007-06-15 19:04:19 +00004632 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Duncan Sands9fac0b52007-06-06 10:05:18 +00004633
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004634 if (MMI && BB->isLandingPad()) {
Duncan Sandsf4070822007-06-15 19:04:19 +00004635 // Add a label to mark the beginning of the landing pad. Deletion of the
4636 // landing pad can thus be detected via the MachineModuleInfo.
4637 unsigned LabelID = MMI->addLandingPad(BB);
4638 DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, DAG.getEntryNode(),
Evan Chengbb81d972008-01-31 09:59:15 +00004639 DAG.getConstant(LabelID, MVT::i32),
4640 DAG.getConstant(1, MVT::i32)));
Duncan Sandsf4070822007-06-15 19:04:19 +00004641
Evan Chenge47c3332007-06-27 18:45:32 +00004642 // Mark exception register as live in.
4643 unsigned Reg = TLI.getExceptionAddressRegister();
4644 if (Reg) BB->addLiveIn(Reg);
4645
4646 // Mark exception selector register as live in.
4647 Reg = TLI.getExceptionSelectorRegister();
4648 if (Reg) BB->addLiveIn(Reg);
4649
Duncan Sandsf4070822007-06-15 19:04:19 +00004650 // FIXME: Hack around an exception handling flaw (PR1508): the personality
4651 // function and list of typeids logically belong to the invoke (or, if you
4652 // like, the basic block containing the invoke), and need to be associated
4653 // with it in the dwarf exception handling tables. Currently however the
Duncan Sandscf26d7c2007-07-04 20:52:51 +00004654 // information is provided by an intrinsic (eh.selector) that can be moved
4655 // to unexpected places by the optimizers: if the unwind edge is critical,
4656 // then breaking it can result in the intrinsics being in the successor of
4657 // the landing pad, not the landing pad itself. This results in exceptions
4658 // not being caught because no typeids are associated with the invoke.
4659 // This may not be the only way things can go wrong, but it is the only way
4660 // we try to work around for the moment.
Duncan Sandsf4070822007-06-15 19:04:19 +00004661 BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator());
4662
4663 if (Br && Br->isUnconditional()) { // Critical edge?
4664 BasicBlock::iterator I, E;
4665 for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I)
Duncan Sandscf26d7c2007-07-04 20:52:51 +00004666 if (isSelector(I))
Duncan Sandsf4070822007-06-15 19:04:19 +00004667 break;
4668
4669 if (I == E)
4670 // No catch info found - try to extract some from the successor.
4671 copyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, FuncInfo);
Duncan Sands9fac0b52007-06-06 10:05:18 +00004672 }
4673 }
4674
Chris Lattner1c08c712005-01-07 07:47:53 +00004675 // Lower all of the non-terminator instructions.
4676 for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
4677 I != E; ++I)
4678 SDL.visit(*I);
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00004679
Chris Lattner1c08c712005-01-07 07:47:53 +00004680 // Ensure that all instructions which are used outside of their defining
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00004681 // blocks are available as virtual registers. Invoke is handled elsewhere.
Chris Lattner1c08c712005-01-07 07:47:53 +00004682 for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00004683 if (!I->use_empty() && !isa<PHINode>(I) && !isa<InvokeInst>(I)) {
Chris Lattner9f24ad72007-02-04 01:35:11 +00004684 DenseMap<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
Chris Lattner1c08c712005-01-07 07:47:53 +00004685 if (VMI != FuncInfo.ValueMap.end())
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004686 SDL.CopyValueToVirtualRegister(I, VMI->second);
Chris Lattner1c08c712005-01-07 07:47:53 +00004687 }
4688
4689 // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
4690 // ensure constants are generated when needed. Remember the virtual registers
4691 // that need to be added to the Machine PHI nodes as input. We cannot just
4692 // directly add them, because expansion might result in multiple MBB's for one
4693 // BB. As such, the start of the BB might correspond to a different MBB than
4694 // the end.
Misha Brukmanedf128a2005-04-21 22:36:52 +00004695 //
Chris Lattner8c494ab2006-10-27 23:50:33 +00004696 TerminatorInst *TI = LLVMBB->getTerminator();
Chris Lattner1c08c712005-01-07 07:47:53 +00004697
4698 // Emit constants only once even if used by multiple PHI nodes.
4699 std::map<Constant*, unsigned> ConstantsOut;
Chris Lattnerd5e93c02006-09-07 01:59:34 +00004700
Chris Lattner8c494ab2006-10-27 23:50:33 +00004701 // Vector bool would be better, but vector<bool> is really slow.
4702 std::vector<unsigned char> SuccsHandled;
4703 if (TI->getNumSuccessors())
4704 SuccsHandled.resize(BB->getParent()->getNumBlockIDs());
4705
Dan Gohman532dc2e2007-07-09 20:59:04 +00004706 // Check successor nodes' PHI nodes that expect a constant to be available
4707 // from this block.
Chris Lattner1c08c712005-01-07 07:47:53 +00004708 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
4709 BasicBlock *SuccBB = TI->getSuccessor(succ);
Chris Lattnerd5e93c02006-09-07 01:59:34 +00004710 if (!isa<PHINode>(SuccBB->begin())) continue;
Chris Lattner8c494ab2006-10-27 23:50:33 +00004711 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Chris Lattnerd5e93c02006-09-07 01:59:34 +00004712
Chris Lattner8c494ab2006-10-27 23:50:33 +00004713 // If this terminator has multiple identical successors (common for
4714 // switches), only handle each succ once.
4715 unsigned SuccMBBNo = SuccMBB->getNumber();
4716 if (SuccsHandled[SuccMBBNo]) continue;
4717 SuccsHandled[SuccMBBNo] = true;
4718
4719 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Chris Lattner1c08c712005-01-07 07:47:53 +00004720 PHINode *PN;
4721
4722 // At this point we know that there is a 1-1 correspondence between LLVM PHI
4723 // nodes and Machine PHI nodes, but the incoming operands have not been
4724 // emitted yet.
4725 for (BasicBlock::iterator I = SuccBB->begin();
Chris Lattner8c494ab2006-10-27 23:50:33 +00004726 (PN = dyn_cast<PHINode>(I)); ++I) {
4727 // Ignore dead phi's.
4728 if (PN->use_empty()) continue;
4729
4730 unsigned Reg;
4731 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Chris Lattner3f7927c2006-11-29 01:12:32 +00004732
Chris Lattner8c494ab2006-10-27 23:50:33 +00004733 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
4734 unsigned &RegOut = ConstantsOut[C];
4735 if (RegOut == 0) {
4736 RegOut = FuncInfo.CreateRegForValue(C);
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004737 SDL.CopyValueToVirtualRegister(C, RegOut);
Chris Lattner1c08c712005-01-07 07:47:53 +00004738 }
Chris Lattner8c494ab2006-10-27 23:50:33 +00004739 Reg = RegOut;
4740 } else {
4741 Reg = FuncInfo.ValueMap[PHIOp];
4742 if (Reg == 0) {
4743 assert(isa<AllocaInst>(PHIOp) &&
4744 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
4745 "Didn't codegen value into a register!??");
4746 Reg = FuncInfo.CreateRegForValue(PHIOp);
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004747 SDL.CopyValueToVirtualRegister(PHIOp, Reg);
Chris Lattner7e021512006-03-31 02:12:18 +00004748 }
Chris Lattner1c08c712005-01-07 07:47:53 +00004749 }
Chris Lattner8c494ab2006-10-27 23:50:33 +00004750
4751 // Remember that this register needs to added to the machine PHI node as
4752 // the input for this MBB.
4753 MVT::ValueType VT = TLI.getValueType(PN->getType());
Dan Gohman7f321562007-06-25 16:23:39 +00004754 unsigned NumRegisters = TLI.getNumRegisters(VT);
Dan Gohmanb9f10192007-06-21 14:42:22 +00004755 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Chris Lattner8c494ab2006-10-27 23:50:33 +00004756 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
4757 }
Chris Lattner1c08c712005-01-07 07:47:53 +00004758 }
4759 ConstantsOut.clear();
4760
4761 // Lower the terminator after the copies are emitted.
Duncan Sandsf19f6bb2007-06-13 05:51:31 +00004762 SDL.visit(*LLVMBB->getTerminator());
Chris Lattnera651cf62005-01-17 19:43:36 +00004763
Nate Begemanf15485a2006-03-27 01:32:24 +00004764 // Copy over any CaseBlock records that may now exist due to SwitchInst
Nate Begeman37efe672006-04-22 18:53:45 +00004765 // lowering, as well as any jump table information.
Nate Begemanf15485a2006-03-27 01:32:24 +00004766 SwitchCases.clear();
4767 SwitchCases = SDL.SwitchCases;
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004768 JTCases.clear();
4769 JTCases = SDL.JTCases;
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004770 BitTestCases.clear();
4771 BitTestCases = SDL.BitTestCases;
4772
Chris Lattnera651cf62005-01-17 19:43:36 +00004773 // Make sure the root of the DAG is up-to-date.
Dan Gohman86e1ebf2008-03-27 19:56:19 +00004774 DAG.setRoot(SDL.getControlRoot());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004775
4776 // Check whether calls in this block are real tail calls. Fix up CALL nodes
4777 // with correct tailcall attribute so that the target can rely on the tailcall
4778 // attribute indicating whether the call is really eligible for tail call
4779 // optimization.
4780 CheckDAGForTailCallsAndFixThem(DAG, TLI);
Chris Lattner1c08c712005-01-07 07:47:53 +00004781}
4782
Nate Begemanf15485a2006-03-27 01:32:24 +00004783void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) {
Dan Gohman417e11b2007-10-08 15:12:17 +00004784 DOUT << "Lowered selection DAG:\n";
4785 DEBUG(DAG.dump());
4786
Chris Lattneraf21d552005-10-10 16:47:10 +00004787 // Run the DAG combiner in pre-legalize mode.
Dan Gohman5f43f922007-08-27 16:26:13 +00004788 DAG.Combine(false, *AA);
Nate Begeman2300f552005-09-07 00:15:36 +00004789
Dan Gohman417e11b2007-10-08 15:12:17 +00004790 DOUT << "Optimized lowered selection DAG:\n";
Chris Lattner1c08c712005-01-07 07:47:53 +00004791 DEBUG(DAG.dump());
Nate Begemanf15485a2006-03-27 01:32:24 +00004792
Chris Lattner1c08c712005-01-07 07:47:53 +00004793 // Second step, hack on the DAG until it only uses operations and types that
4794 // the target supports.
Chris Lattner01d029b2007-10-15 06:10:22 +00004795#if 0 // Enable this some day.
4796 DAG.LegalizeTypes();
4797 // Someday even later, enable a dag combine pass here.
4798#endif
Chris Lattnerac9dc082005-01-23 04:36:26 +00004799 DAG.Legalize();
Nate Begemanf15485a2006-03-27 01:32:24 +00004800
Bill Wendling832171c2006-12-07 20:04:42 +00004801 DOUT << "Legalized selection DAG:\n";
Chris Lattner1c08c712005-01-07 07:47:53 +00004802 DEBUG(DAG.dump());
Nate Begemanf15485a2006-03-27 01:32:24 +00004803
Chris Lattneraf21d552005-10-10 16:47:10 +00004804 // Run the DAG combiner in post-legalize mode.
Dan Gohman5f43f922007-08-27 16:26:13 +00004805 DAG.Combine(true, *AA);
Nate Begeman2300f552005-09-07 00:15:36 +00004806
Dan Gohman417e11b2007-10-08 15:12:17 +00004807 DOUT << "Optimized legalized selection DAG:\n";
4808 DEBUG(DAG.dump());
4809
Evan Chenga9c20912006-01-21 02:32:06 +00004810 if (ViewISelDAGs) DAG.viewGraph();
Evan Cheng552c4a82006-04-28 02:09:19 +00004811
Chris Lattnera33ef482005-03-30 01:10:47 +00004812 // Third, instruction select all of the operations to machine code, adding the
4813 // code to the MachineBasicBlock.
Chris Lattner1c08c712005-01-07 07:47:53 +00004814 InstructionSelectBasicBlock(DAG);
Nate Begemanf15485a2006-03-27 01:32:24 +00004815
Bill Wendling832171c2006-12-07 20:04:42 +00004816 DOUT << "Selected machine code:\n";
Chris Lattner1c08c712005-01-07 07:47:53 +00004817 DEBUG(BB->dump());
Nate Begemanf15485a2006-03-27 01:32:24 +00004818}
Chris Lattner1c08c712005-01-07 07:47:53 +00004819
Nate Begemanf15485a2006-03-27 01:32:24 +00004820void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
4821 FunctionLoweringInfo &FuncInfo) {
4822 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
4823 {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00004824 SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
Nate Begemanf15485a2006-03-27 01:32:24 +00004825 CurDAG = &DAG;
4826
4827 // First step, lower LLVM code to some DAG. This DAG may use operations and
4828 // types that are not supported by the target.
4829 BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
4830
4831 // Second step, emit the lowered DAG as machine code.
4832 CodeGenAndEmitDAG(DAG);
4833 }
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004834
4835 DOUT << "Total amount of phi nodes to update: "
4836 << PHINodesToUpdate.size() << "\n";
4837 DEBUG(for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i)
4838 DOUT << "Node " << i << " : (" << PHINodesToUpdate[i].first
4839 << ", " << PHINodesToUpdate[i].second << ")\n";);
Nate Begemanf15485a2006-03-27 01:32:24 +00004840
Chris Lattnera33ef482005-03-30 01:10:47 +00004841 // Next, now that we know what the last MBB the LLVM BB expanded is, update
Chris Lattner1c08c712005-01-07 07:47:53 +00004842 // PHI nodes in successors.
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004843 if (SwitchCases.empty() && JTCases.empty() && BitTestCases.empty()) {
Nate Begemanf15485a2006-03-27 01:32:24 +00004844 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
4845 MachineInstr *PHI = PHINodesToUpdate[i].first;
4846 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4847 "This is not a machine PHI node that we are updating!");
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00004848 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[i].second,
4849 false));
4850 PHI->addOperand(MachineOperand::CreateMBB(BB));
Nate Begemanf15485a2006-03-27 01:32:24 +00004851 }
4852 return;
Chris Lattner1c08c712005-01-07 07:47:53 +00004853 }
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004854
4855 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i) {
4856 // Lower header first, if it wasn't already lowered
4857 if (!BitTestCases[i].Emitted) {
4858 SelectionDAG HSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4859 CurDAG = &HSDAG;
Gordon Henriksence224772008-01-07 01:30:38 +00004860 SelectionDAGLowering HSDL(HSDAG, TLI, *AA, FuncInfo, GCI);
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004861 // Set the current basic block to the mbb we wish to insert the code into
4862 BB = BitTestCases[i].Parent;
4863 HSDL.setCurrentBasicBlock(BB);
4864 // Emit the code
4865 HSDL.visitBitTestHeader(BitTestCases[i]);
4866 HSDAG.setRoot(HSDL.getRoot());
4867 CodeGenAndEmitDAG(HSDAG);
4868 }
4869
4870 for (unsigned j = 0, ej = BitTestCases[i].Cases.size(); j != ej; ++j) {
4871 SelectionDAG BSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4872 CurDAG = &BSDAG;
Gordon Henriksence224772008-01-07 01:30:38 +00004873 SelectionDAGLowering BSDL(BSDAG, TLI, *AA, FuncInfo, GCI);
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004874 // Set the current basic block to the mbb we wish to insert the code into
4875 BB = BitTestCases[i].Cases[j].ThisBB;
4876 BSDL.setCurrentBasicBlock(BB);
4877 // Emit the code
4878 if (j+1 != ej)
4879 BSDL.visitBitTestCase(BitTestCases[i].Cases[j+1].ThisBB,
4880 BitTestCases[i].Reg,
4881 BitTestCases[i].Cases[j]);
4882 else
4883 BSDL.visitBitTestCase(BitTestCases[i].Default,
4884 BitTestCases[i].Reg,
4885 BitTestCases[i].Cases[j]);
4886
4887
4888 BSDAG.setRoot(BSDL.getRoot());
4889 CodeGenAndEmitDAG(BSDAG);
4890 }
4891
4892 // Update PHI Nodes
4893 for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) {
4894 MachineInstr *PHI = PHINodesToUpdate[pi].first;
4895 MachineBasicBlock *PHIBB = PHI->getParent();
4896 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4897 "This is not a machine PHI node that we are updating!");
4898 // This is "default" BB. We have two jumps to it. From "header" BB and
4899 // from last "case" BB.
4900 if (PHIBB == BitTestCases[i].Default) {
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00004901 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second,
4902 false));
4903 PHI->addOperand(MachineOperand::CreateMBB(BitTestCases[i].Parent));
4904 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second,
4905 false));
4906 PHI->addOperand(MachineOperand::CreateMBB(BitTestCases[i].Cases.
4907 back().ThisBB));
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004908 }
4909 // One of "cases" BB.
4910 for (unsigned j = 0, ej = BitTestCases[i].Cases.size(); j != ej; ++j) {
4911 MachineBasicBlock* cBB = BitTestCases[i].Cases[j].ThisBB;
4912 if (cBB->succ_end() !=
4913 std::find(cBB->succ_begin(),cBB->succ_end(), PHIBB)) {
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00004914 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second,
4915 false));
4916 PHI->addOperand(MachineOperand::CreateMBB(cBB));
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004917 }
4918 }
4919 }
4920 }
4921
Nate Begeman9453eea2006-04-23 06:26:20 +00004922 // If the JumpTable record is filled in, then we need to emit a jump table.
4923 // Updating the PHI nodes is tricky in this case, since we need to determine
4924 // whether the PHI is a successor of the range check MBB or the jump table MBB
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004925 for (unsigned i = 0, e = JTCases.size(); i != e; ++i) {
4926 // Lower header first, if it wasn't already lowered
4927 if (!JTCases[i].first.Emitted) {
4928 SelectionDAG HSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4929 CurDAG = &HSDAG;
Gordon Henriksence224772008-01-07 01:30:38 +00004930 SelectionDAGLowering HSDL(HSDAG, TLI, *AA, FuncInfo, GCI);
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004931 // Set the current basic block to the mbb we wish to insert the code into
4932 BB = JTCases[i].first.HeaderBB;
4933 HSDL.setCurrentBasicBlock(BB);
4934 // Emit the code
4935 HSDL.visitJumpTableHeader(JTCases[i].second, JTCases[i].first);
4936 HSDAG.setRoot(HSDL.getRoot());
4937 CodeGenAndEmitDAG(HSDAG);
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004938 }
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004939
4940 SelectionDAG JSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4941 CurDAG = &JSDAG;
Gordon Henriksence224772008-01-07 01:30:38 +00004942 SelectionDAGLowering JSDL(JSDAG, TLI, *AA, FuncInfo, GCI);
Nate Begeman37efe672006-04-22 18:53:45 +00004943 // Set the current basic block to the mbb we wish to insert the code into
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004944 BB = JTCases[i].second.MBB;
4945 JSDL.setCurrentBasicBlock(BB);
Nate Begeman37efe672006-04-22 18:53:45 +00004946 // Emit the code
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004947 JSDL.visitJumpTable(JTCases[i].second);
4948 JSDAG.setRoot(JSDL.getRoot());
4949 CodeGenAndEmitDAG(JSDAG);
4950
Nate Begeman37efe672006-04-22 18:53:45 +00004951 // Update PHI Nodes
4952 for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) {
4953 MachineInstr *PHI = PHINodesToUpdate[pi].first;
4954 MachineBasicBlock *PHIBB = PHI->getParent();
4955 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4956 "This is not a machine PHI node that we are updating!");
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004957 // "default" BB. We can go there only from header BB.
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +00004958 if (PHIBB == JTCases[i].second.Default) {
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00004959 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second,
4960 false));
4961 PHI->addOperand(MachineOperand::CreateMBB(JTCases[i].first.HeaderBB));
Nate Begemanf4360a42006-05-03 03:48:02 +00004962 }
Anton Korobeynikov4198c582007-04-09 12:31:58 +00004963 // JT BB. Just iterate over successors here
Nate Begemanf4360a42006-05-03 03:48:02 +00004964 if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) {
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00004965 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second,
4966 false));
4967 PHI->addOperand(MachineOperand::CreateMBB(BB));
Nate Begeman37efe672006-04-22 18:53:45 +00004968 }
4969 }
Nate Begeman37efe672006-04-22 18:53:45 +00004970 }
4971
Chris Lattnerb2e806e2006-10-22 23:00:53 +00004972 // If the switch block involved a branch to one of the actual successors, we
4973 // need to update PHI nodes in that block.
4974 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
4975 MachineInstr *PHI = PHINodesToUpdate[i].first;
4976 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4977 "This is not a machine PHI node that we are updating!");
4978 if (BB->isSuccessor(PHI->getParent())) {
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00004979 PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[i].second,
4980 false));
4981 PHI->addOperand(MachineOperand::CreateMBB(BB));
Chris Lattnerb2e806e2006-10-22 23:00:53 +00004982 }
4983 }
4984
Nate Begemanf15485a2006-03-27 01:32:24 +00004985 // If we generated any switch lowering information, build and codegen any
4986 // additional DAGs necessary.
Chris Lattnerd5e93c02006-09-07 01:59:34 +00004987 for (unsigned i = 0, e = SwitchCases.size(); i != e; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00004988 SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
Nate Begemanf15485a2006-03-27 01:32:24 +00004989 CurDAG = &SDAG;
Gordon Henriksence224772008-01-07 01:30:38 +00004990 SelectionDAGLowering SDL(SDAG, TLI, *AA, FuncInfo, GCI);
Chris Lattnerd5e93c02006-09-07 01:59:34 +00004991
Nate Begemanf15485a2006-03-27 01:32:24 +00004992 // Set the current basic block to the mbb we wish to insert the code into
4993 BB = SwitchCases[i].ThisBB;
4994 SDL.setCurrentBasicBlock(BB);
Chris Lattnerd5e93c02006-09-07 01:59:34 +00004995
Nate Begemanf15485a2006-03-27 01:32:24 +00004996 // Emit the code
4997 SDL.visitSwitchCase(SwitchCases[i]);
4998 SDAG.setRoot(SDL.getRoot());
4999 CodeGenAndEmitDAG(SDAG);
Chris Lattnerd5e93c02006-09-07 01:59:34 +00005000
5001 // Handle any PHI nodes in successors of this chunk, as if we were coming
5002 // from the original BB before switch expansion. Note that PHI nodes can
5003 // occur multiple times in PHINodesToUpdate. We have to be very careful to
5004 // handle them the right number of times.
Chris Lattner57ab6592006-10-24 17:57:59 +00005005 while ((BB = SwitchCases[i].TrueBB)) { // Handle LHS and RHS.
Chris Lattnerd5e93c02006-09-07 01:59:34 +00005006 for (MachineBasicBlock::iterator Phi = BB->begin();
5007 Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){
5008 // This value for this PHI node is recorded in PHINodesToUpdate, get it.
5009 for (unsigned pn = 0; ; ++pn) {
5010 assert(pn != PHINodesToUpdate.size() && "Didn't find PHI entry!");
5011 if (PHINodesToUpdate[pn].first == Phi) {
Chris Lattner9ce2e9d2007-12-30 00:57:42 +00005012 Phi->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pn].
5013 second, false));
5014 Phi->addOperand(MachineOperand::CreateMBB(SwitchCases[i].ThisBB));
Chris Lattnerd5e93c02006-09-07 01:59:34 +00005015 break;
5016 }
5017 }
Nate Begemanf15485a2006-03-27 01:32:24 +00005018 }
Chris Lattnerd5e93c02006-09-07 01:59:34 +00005019
5020 // Don't process RHS if same block as LHS.
Chris Lattner57ab6592006-10-24 17:57:59 +00005021 if (BB == SwitchCases[i].FalseBB)
5022 SwitchCases[i].FalseBB = 0;
Chris Lattnerd5e93c02006-09-07 01:59:34 +00005023
5024 // If we haven't handled the RHS, do so now. Otherwise, we're done.
Chris Lattner24525952006-10-24 18:07:37 +00005025 SwitchCases[i].TrueBB = SwitchCases[i].FalseBB;
Chris Lattner57ab6592006-10-24 17:57:59 +00005026 SwitchCases[i].FalseBB = 0;
Nate Begemanf15485a2006-03-27 01:32:24 +00005027 }
Chris Lattner57ab6592006-10-24 17:57:59 +00005028 assert(SwitchCases[i].TrueBB == 0 && SwitchCases[i].FalseBB == 0);
Chris Lattnera33ef482005-03-30 01:10:47 +00005029 }
Chris Lattner1c08c712005-01-07 07:47:53 +00005030}
Evan Chenga9c20912006-01-21 02:32:06 +00005031
Jim Laskey13ec7022006-08-01 14:21:23 +00005032
Evan Chenga9c20912006-01-21 02:32:06 +00005033//===----------------------------------------------------------------------===//
5034/// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each
5035/// target node in the graph.
5036void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) {
5037 if (ViewSchedDAGs) DAG.viewGraph();
Evan Cheng4ef10862006-01-23 07:01:07 +00005038
Jim Laskeyeb577ba2006-08-02 12:30:23 +00005039 RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
Jim Laskey13ec7022006-08-01 14:21:23 +00005040
5041 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +00005042 Ctor = ISHeuristic;
Jim Laskey9373beb2006-08-01 19:14:14 +00005043 RegisterScheduler::setDefault(Ctor);
Evan Cheng4ef10862006-01-23 07:01:07 +00005044 }
Jim Laskey13ec7022006-08-01 14:21:23 +00005045
Jim Laskey9ff542f2006-08-01 18:29:48 +00005046 ScheduleDAG *SL = Ctor(this, &DAG, BB);
Chris Lattnera3818e62006-01-21 19:12:11 +00005047 BB = SL->Run();
Dan Gohman3e1a7ae2007-08-28 20:32:58 +00005048
5049 if (ViewSUnitDAGs) SL->viewGraph();
5050
Evan Chengcccf1232006-02-04 06:49:00 +00005051 delete SL;
Evan Chenga9c20912006-01-21 02:32:06 +00005052}
Chris Lattner0e43f2b2006-02-24 02:13:54 +00005053
Chris Lattner03fc53c2006-03-06 00:22:00 +00005054
Jim Laskey9ff542f2006-08-01 18:29:48 +00005055HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
5056 return new HazardRecognizer();
5057}
5058
Chris Lattner75548062006-10-11 03:58:02 +00005059//===----------------------------------------------------------------------===//
5060// Helper functions used by the generated instruction selector.
5061//===----------------------------------------------------------------------===//
5062// Calls to these methods are generated by tblgen.
5063
5064/// CheckAndMask - The isel is trying to match something like (and X, 255). If
5065/// the dag combiner simplified the 255, we still want to match. RHS is the
5066/// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
5067/// specified in the .td file (e.g. 255).
5068bool SelectionDAGISel::CheckAndMask(SDOperand LHS, ConstantSDNode *RHS,
Dan Gohmandc9b3d02007-07-24 23:00:27 +00005069 int64_t DesiredMaskS) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005070 const APInt &ActualMask = RHS->getAPIntValue();
5071 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
Chris Lattner75548062006-10-11 03:58:02 +00005072
5073 // If the actual mask exactly matches, success!
5074 if (ActualMask == DesiredMask)
5075 return true;
5076
5077 // If the actual AND mask is allowing unallowed bits, this doesn't match.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005078 if (ActualMask.intersects(~DesiredMask))
Chris Lattner75548062006-10-11 03:58:02 +00005079 return false;
5080
5081 // Otherwise, the DAG Combiner may have proven that the value coming in is
5082 // either already zero or is not demanded. Check for known zero input bits.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005083 APInt NeededMask = DesiredMask & ~ActualMask;
Dan Gohmanea859be2007-06-22 14:59:07 +00005084 if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
Chris Lattner75548062006-10-11 03:58:02 +00005085 return true;
5086
5087 // TODO: check to see if missing bits are just not demanded.
5088
5089 // Otherwise, this pattern doesn't match.
5090 return false;
5091}
5092
5093/// CheckOrMask - The isel is trying to match something like (or X, 255). If
5094/// the dag combiner simplified the 255, we still want to match. RHS is the
5095/// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
5096/// specified in the .td file (e.g. 255).
5097bool SelectionDAGISel::CheckOrMask(SDOperand LHS, ConstantSDNode *RHS,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005098 int64_t DesiredMaskS) const {
5099 const APInt &ActualMask = RHS->getAPIntValue();
5100 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
Chris Lattner75548062006-10-11 03:58:02 +00005101
5102 // If the actual mask exactly matches, success!
5103 if (ActualMask == DesiredMask)
5104 return true;
5105
5106 // If the actual AND mask is allowing unallowed bits, this doesn't match.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005107 if (ActualMask.intersects(~DesiredMask))
Chris Lattner75548062006-10-11 03:58:02 +00005108 return false;
5109
5110 // Otherwise, the DAG Combiner may have proven that the value coming in is
5111 // either already zero or is not demanded. Check for known zero input bits.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005112 APInt NeededMask = DesiredMask & ~ActualMask;
Chris Lattner75548062006-10-11 03:58:02 +00005113
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005114 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005115 CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
Chris Lattner75548062006-10-11 03:58:02 +00005116
5117 // If all the missing bits in the or are already known to be set, match!
5118 if ((NeededMask & KnownOne) == NeededMask)
5119 return true;
5120
5121 // TODO: check to see if missing bits are just not demanded.
5122
5123 // Otherwise, this pattern doesn't match.
5124 return false;
5125}
5126
Jim Laskey9ff542f2006-08-01 18:29:48 +00005127
Chris Lattner0e43f2b2006-02-24 02:13:54 +00005128/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
5129/// by tblgen. Others should not call it.
5130void SelectionDAGISel::
5131SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) {
5132 std::vector<SDOperand> InOps;
5133 std::swap(InOps, Ops);
5134
5135 Ops.push_back(InOps[0]); // input chain.
5136 Ops.push_back(InOps[1]); // input asm string.
5137
Chris Lattner0e43f2b2006-02-24 02:13:54 +00005138 unsigned i = 2, e = InOps.size();
5139 if (InOps[e-1].getValueType() == MVT::Flag)
5140 --e; // Don't process a flag operand if it is here.
5141
5142 while (i != e) {
5143 unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue();
5144 if ((Flags & 7) != 4 /*MEM*/) {
5145 // Just skip over this operand, copying the operands verbatim.
5146 Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1);
5147 i += (Flags >> 3) + 1;
5148 } else {
5149 assert((Flags >> 3) == 1 && "Memory operand with multiple values?");
5150 // Otherwise, this is a memory operand. Ask the target to select it.
5151 std::vector<SDOperand> SelOps;
5152 if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005153 cerr << "Could not match memory address. Inline asm failure!\n";
Chris Lattner0e43f2b2006-02-24 02:13:54 +00005154 exit(1);
5155 }
5156
5157 // Add this to the output node.
Chris Lattner4b993b12007-04-09 00:33:58 +00005158 MVT::ValueType IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Chris Lattner36d43962006-12-16 21:14:48 +00005159 Ops.push_back(DAG.getTargetConstant(4/*MEM*/ | (SelOps.size() << 3),
Chris Lattner4b993b12007-04-09 00:33:58 +00005160 IntPtrTy));
Chris Lattner0e43f2b2006-02-24 02:13:54 +00005161 Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
5162 i += 2;
5163 }
5164 }
5165
5166 // Add the flag input back if present.
5167 if (e != InOps.size())
5168 Ops.push_back(InOps.back());
5169}
Devang Patel794fd752007-05-01 21:15:47 +00005170
Devang Patel19974732007-05-03 01:11:54 +00005171char SelectionDAGISel::ID = 0;