blob: 512a62deff8a9d7253c8db51b463cf3722141ae3 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
15#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000016#include "llvm/CallingConv.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Function.h"
20#include "llvm/Instructions.h"
21#include "llvm/Intrinsics.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetFrameInfo.h"
29#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetLowering.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner7944d9d2005-01-12 03:41:21 +000032#include "llvm/Support/CommandLine.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000033#include "llvm/Support/Debug.h"
34#include <map>
35#include <iostream>
36using namespace llvm;
37
Chris Lattner7944d9d2005-01-12 03:41:21 +000038#ifndef _NDEBUG
39static cl::opt<bool>
40ViewDAGs("view-isel-dags", cl::Hidden,
41 cl::desc("Pop up a window to show isel dags as they are selected"));
42#else
43static const bool ViewDAGS = 0;
44#endif
45
Chris Lattner1c08c712005-01-07 07:47:53 +000046namespace llvm {
47 //===--------------------------------------------------------------------===//
48 /// FunctionLoweringInfo - This contains information that is global to a
49 /// function that is used when lowering a region of the function.
Chris Lattnerf26bc8e2005-01-08 19:52:31 +000050 class FunctionLoweringInfo {
51 public:
Chris Lattner1c08c712005-01-07 07:47:53 +000052 TargetLowering &TLI;
53 Function &Fn;
54 MachineFunction &MF;
55 SSARegMap *RegMap;
56
57 FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
58
59 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
60 std::map<const BasicBlock*, MachineBasicBlock *> MBBMap;
61
62 /// ValueMap - Since we emit code for the function a basic block at a time,
63 /// we must remember which virtual registers hold the values for
64 /// cross-basic-block values.
65 std::map<const Value*, unsigned> ValueMap;
66
67 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
68 /// the entry block. This allows the allocas to be efficiently referenced
69 /// anywhere in the function.
70 std::map<const AllocaInst*, int> StaticAllocaMap;
71
Chris Lattner0afa8e32005-01-17 17:55:19 +000072 /// BlockLocalArguments - If any arguments are only used in a single basic
73 /// block, and if the target can access the arguments without side-effects,
74 /// avoid emitting CopyToReg nodes for those arguments. This map keeps
75 /// track of which arguments are local to each BB.
76 std::multimap<BasicBlock*, std::pair<Argument*,
77 unsigned> > BlockLocalArguments;
78
79
Chris Lattner1c08c712005-01-07 07:47:53 +000080 unsigned MakeReg(MVT::ValueType VT) {
81 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
82 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000083
Chris Lattner1c08c712005-01-07 07:47:53 +000084 unsigned CreateRegForValue(const Value *V) {
85 MVT::ValueType VT = TLI.getValueType(V->getType());
86 // The common case is that we will only create one register for this
87 // value. If we have that case, create and return the virtual register.
88 unsigned NV = TLI.getNumElements(VT);
Chris Lattnerfb849802005-01-16 00:37:38 +000089 if (NV == 1) {
90 // If we are promoting this value, pick the next largest supported type.
Chris Lattner98e5c0e2005-01-16 01:11:19 +000091 return MakeReg(TLI.getTypeToTransformTo(VT));
Chris Lattnerfb849802005-01-16 00:37:38 +000092 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000093
Chris Lattner1c08c712005-01-07 07:47:53 +000094 // If this value is represented with multiple target registers, make sure
95 // to create enough consequtive registers of the right (smaller) type.
96 unsigned NT = VT-1; // Find the type to use.
97 while (TLI.getNumElements((MVT::ValueType)NT) != 1)
98 --NT;
Misha Brukmanedf128a2005-04-21 22:36:52 +000099
Chris Lattner1c08c712005-01-07 07:47:53 +0000100 unsigned R = MakeReg((MVT::ValueType)NT);
101 for (unsigned i = 1; i != NV; ++i)
102 MakeReg((MVT::ValueType)NT);
103 return R;
104 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000105
Chris Lattner1c08c712005-01-07 07:47:53 +0000106 unsigned InitializeRegForValue(const Value *V) {
107 unsigned &R = ValueMap[V];
108 assert(R == 0 && "Already initialized this value register!");
109 return R = CreateRegForValue(V);
110 }
111 };
112}
113
114/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
115/// PHI nodes or outside of the basic block that defines it.
116static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
117 if (isa<PHINode>(I)) return true;
118 BasicBlock *BB = I->getParent();
119 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
120 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
121 return true;
122 return false;
123}
124
125FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000126 Function &fn, MachineFunction &mf)
Chris Lattner1c08c712005-01-07 07:47:53 +0000127 : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
128
129 // Initialize the mapping of values to registers. This is only set up for
130 // instruction values that are used outside of the block that defines
131 // them.
Chris Lattner16ce0df2005-05-11 18:57:06 +0000132 for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end();
133 AI != E; ++AI)
Chris Lattner1c08c712005-01-07 07:47:53 +0000134 InitializeRegForValue(AI);
135
136 Function::iterator BB = Fn.begin(), E = Fn.end();
137 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
138 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
139 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) {
140 const Type *Ty = AI->getAllocatedType();
141 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
142 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
Chris Lattnera8217e32005-05-13 23:14:17 +0000143
144 // If the alignment of the value is smaller than the size of the value,
145 // and if the size of the value is particularly small (<= 8 bytes),
146 // round up to the size of the value for potentially better performance.
147 //
148 // FIXME: This could be made better with a preferred alignment hook in
149 // TargetData. It serves primarily to 8-byte align doubles for X86.
150 if (Align < TySize && TySize <= 8) Align = TySize;
151
Chris Lattner1c08c712005-01-07 07:47:53 +0000152 TySize *= CUI->getValue(); // Get total allocated size.
153 StaticAllocaMap[AI] =
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000154 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
Chris Lattner1c08c712005-01-07 07:47:53 +0000155 }
156
157 for (; BB != E; ++BB)
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000158 for (BasicBlock::iterator I = BB->begin(), e = BB->end(); I != e; ++I)
Chris Lattner1c08c712005-01-07 07:47:53 +0000159 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
160 if (!isa<AllocaInst>(I) ||
161 !StaticAllocaMap.count(cast<AllocaInst>(I)))
162 InitializeRegForValue(I);
163
164 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
165 // also creates the initial PHI MachineInstrs, though none of the input
166 // operands are populated.
167 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
168 MachineBasicBlock *MBB = new MachineBasicBlock(BB);
169 MBBMap[BB] = MBB;
170 MF.getBasicBlockList().push_back(MBB);
171
172 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
173 // appropriate.
174 PHINode *PN;
175 for (BasicBlock::iterator I = BB->begin();
Chris Lattnerf44fd882005-01-07 21:34:19 +0000176 (PN = dyn_cast<PHINode>(I)); ++I)
177 if (!PN->use_empty()) {
178 unsigned NumElements =
179 TLI.getNumElements(TLI.getValueType(PN->getType()));
180 unsigned PHIReg = ValueMap[PN];
181 assert(PHIReg &&"PHI node does not have an assigned virtual register!");
182 for (unsigned i = 0; i != NumElements; ++i)
183 BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i);
184 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000185 }
186}
187
188
189
190//===----------------------------------------------------------------------===//
191/// SelectionDAGLowering - This is the common target-independent lowering
192/// implementation that is parameterized by a TargetLowering object.
193/// Also, targets can overload any lowering method.
194///
195namespace llvm {
196class SelectionDAGLowering {
197 MachineBasicBlock *CurMBB;
198
199 std::map<const Value*, SDOperand> NodeMap;
200
Chris Lattnerd3948112005-01-17 22:19:26 +0000201 /// PendingLoads - Loads are not emitted to the program immediately. We bunch
202 /// them up and then emit token factor nodes when possible. This allows us to
203 /// get simple disambiguation between loads without worrying about alias
204 /// analysis.
205 std::vector<SDOperand> PendingLoads;
206
Chris Lattner1c08c712005-01-07 07:47:53 +0000207public:
208 // TLI - This is information that describes the available target features we
209 // need for lowering. This indicates when operations are unavailable,
210 // implemented with a libcall, etc.
211 TargetLowering &TLI;
212 SelectionDAG &DAG;
213 const TargetData &TD;
214
215 /// FuncInfo - Information about the function as a whole.
216 ///
217 FunctionLoweringInfo &FuncInfo;
218
219 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000220 FunctionLoweringInfo &funcinfo)
Chris Lattner1c08c712005-01-07 07:47:53 +0000221 : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
222 FuncInfo(funcinfo) {
223 }
224
Chris Lattnera651cf62005-01-17 19:43:36 +0000225 /// getRoot - Return the current virtual root of the Selection DAG.
226 ///
227 SDOperand getRoot() {
Chris Lattnerd3948112005-01-17 22:19:26 +0000228 if (PendingLoads.empty())
229 return DAG.getRoot();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000230
Chris Lattnerd3948112005-01-17 22:19:26 +0000231 if (PendingLoads.size() == 1) {
232 SDOperand Root = PendingLoads[0];
233 DAG.setRoot(Root);
234 PendingLoads.clear();
235 return Root;
236 }
237
238 // Otherwise, we have to make a token factor node.
239 SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads);
240 PendingLoads.clear();
241 DAG.setRoot(Root);
242 return Root;
Chris Lattnera651cf62005-01-17 19:43:36 +0000243 }
244
Chris Lattner1c08c712005-01-07 07:47:53 +0000245 void visit(Instruction &I) { visit(I.getOpcode(), I); }
246
247 void visit(unsigned Opcode, User &I) {
248 switch (Opcode) {
249 default: assert(0 && "Unknown instruction type encountered!");
250 abort();
251 // Build the switch statement using the Instruction.def file.
252#define HANDLE_INST(NUM, OPCODE, CLASS) \
253 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
254#include "llvm/Instruction.def"
255 }
256 }
257
258 void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
259
260
261 SDOperand getIntPtrConstant(uint64_t Val) {
262 return DAG.getConstant(Val, TLI.getPointerTy());
263 }
264
265 SDOperand getValue(const Value *V) {
266 SDOperand &N = NodeMap[V];
267 if (N.Val) return N;
268
269 MVT::ValueType VT = TLI.getValueType(V->getType());
270 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)))
271 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
272 visit(CE->getOpcode(), *CE);
273 assert(N.Val && "visit didn't populate the ValueMap!");
274 return N;
275 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
276 return N = DAG.getGlobalAddress(GV, VT);
277 } else if (isa<ConstantPointerNull>(C)) {
278 return N = DAG.getConstant(0, TLI.getPointerTy());
279 } else if (isa<UndefValue>(C)) {
Nate Begemanb8827522005-04-12 23:12:17 +0000280 return N = DAG.getNode(ISD::UNDEF, VT);
Chris Lattner1c08c712005-01-07 07:47:53 +0000281 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
282 return N = DAG.getConstantFP(CFP->getValue(), VT);
283 } else {
284 // Canonicalize all constant ints to be unsigned.
285 return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT);
286 }
287
288 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
289 std::map<const AllocaInst*, int>::iterator SI =
290 FuncInfo.StaticAllocaMap.find(AI);
291 if (SI != FuncInfo.StaticAllocaMap.end())
292 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
293 }
294
295 std::map<const Value*, unsigned>::const_iterator VMI =
296 FuncInfo.ValueMap.find(V);
297 assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!");
Chris Lattnerc8ea3c42005-01-16 02:23:07 +0000298
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000299 return N = DAG.getCopyFromReg(VMI->second, VT, DAG.getEntryNode());
Chris Lattner1c08c712005-01-07 07:47:53 +0000300 }
301
302 const SDOperand &setValue(const Value *V, SDOperand NewN) {
303 SDOperand &N = NodeMap[V];
304 assert(N.Val == 0 && "Already set a value for this node!");
305 return N = NewN;
306 }
307
308 // Terminator instructions.
309 void visitRet(ReturnInst &I);
310 void visitBr(BranchInst &I);
311 void visitUnreachable(UnreachableInst &I) { /* noop */ }
312
313 // These all get lowered before this pass.
314 void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); }
315 void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); }
316 void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); }
317
318 //
319 void visitBinary(User &I, unsigned Opcode);
320 void visitAdd(User &I) { visitBinary(I, ISD::ADD); }
Chris Lattnerb9fccc42005-04-02 05:04:50 +0000321 void visitSub(User &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000322 void visitMul(User &I) { visitBinary(I, ISD::MUL); }
323 void visitDiv(User &I) {
324 visitBinary(I, I.getType()->isUnsigned() ? ISD::UDIV : ISD::SDIV);
325 }
326 void visitRem(User &I) {
327 visitBinary(I, I.getType()->isUnsigned() ? ISD::UREM : ISD::SREM);
328 }
329 void visitAnd(User &I) { visitBinary(I, ISD::AND); }
330 void visitOr (User &I) { visitBinary(I, ISD::OR); }
331 void visitXor(User &I) { visitBinary(I, ISD::XOR); }
332 void visitShl(User &I) { visitBinary(I, ISD::SHL); }
333 void visitShr(User &I) {
334 visitBinary(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA);
335 }
336
337 void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc);
338 void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); }
339 void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); }
340 void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); }
341 void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); }
342 void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); }
343 void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); }
344
345 void visitGetElementPtr(User &I);
346 void visitCast(User &I);
347 void visitSelect(User &I);
348 //
349
350 void visitMalloc(MallocInst &I);
351 void visitFree(FreeInst &I);
352 void visitAlloca(AllocaInst &I);
353 void visitLoad(LoadInst &I);
354 void visitStore(StoreInst &I);
355 void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
356 void visitCall(CallInst &I);
357
Chris Lattner1c08c712005-01-07 07:47:53 +0000358 void visitVAStart(CallInst &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000359 void visitVAArg(VAArgInst &I);
360 void visitVAEnd(CallInst &I);
361 void visitVACopy(CallInst &I);
Chris Lattner39ae3622005-01-09 00:00:49 +0000362 void visitFrameReturnAddress(CallInst &I, bool isFrameAddress);
Chris Lattner1c08c712005-01-07 07:47:53 +0000363
Chris Lattner7041ee32005-01-11 05:56:49 +0000364 void visitMemIntrinsic(CallInst &I, unsigned Op);
Chris Lattner1c08c712005-01-07 07:47:53 +0000365
366 void visitUserOp1(Instruction &I) {
367 assert(0 && "UserOp1 should not exist at instruction selection time!");
368 abort();
369 }
370 void visitUserOp2(Instruction &I) {
371 assert(0 && "UserOp2 should not exist at instruction selection time!");
372 abort();
373 }
374};
375} // end namespace llvm
376
377void SelectionDAGLowering::visitRet(ReturnInst &I) {
378 if (I.getNumOperands() == 0) {
Chris Lattnera651cf62005-01-17 19:43:36 +0000379 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot()));
Chris Lattner1c08c712005-01-07 07:47:53 +0000380 return;
381 }
382
383 SDOperand Op1 = getValue(I.getOperand(0));
Chris Lattnerf51d3bd2005-03-29 19:09:56 +0000384 MVT::ValueType TmpVT;
385
Chris Lattner1c08c712005-01-07 07:47:53 +0000386 switch (Op1.getValueType()) {
387 default: assert(0 && "Unknown value type!");
388 case MVT::i1:
389 case MVT::i8:
390 case MVT::i16:
Chris Lattnerf51d3bd2005-03-29 19:09:56 +0000391 case MVT::i32:
392 // If this is a machine where 32-bits is legal or expanded, promote to
393 // 32-bits, otherwise, promote to 64-bits.
394 if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote)
395 TmpVT = TLI.getTypeToTransformTo(MVT::i32);
Chris Lattner1c08c712005-01-07 07:47:53 +0000396 else
Chris Lattnerf51d3bd2005-03-29 19:09:56 +0000397 TmpVT = MVT::i32;
398
399 // Extend integer types to result type.
400 if (I.getOperand(0)->getType()->isSigned())
401 Op1 = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, Op1);
402 else
403 Op1 = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, Op1);
Chris Lattner1c08c712005-01-07 07:47:53 +0000404 break;
405 case MVT::f32:
Chris Lattner1c08c712005-01-07 07:47:53 +0000406 case MVT::i64:
407 case MVT::f64:
408 break; // No extension needed!
409 }
410
Chris Lattnera651cf62005-01-17 19:43:36 +0000411 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot(), Op1));
Chris Lattner1c08c712005-01-07 07:47:53 +0000412}
413
414void SelectionDAGLowering::visitBr(BranchInst &I) {
415 // Update machine-CFG edges.
416 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
Chris Lattner1c08c712005-01-07 07:47:53 +0000417
418 // Figure out which block is immediately after the current one.
419 MachineBasicBlock *NextBlock = 0;
420 MachineFunction::iterator BBI = CurMBB;
421 if (++BBI != CurMBB->getParent()->end())
422 NextBlock = BBI;
423
424 if (I.isUnconditional()) {
425 // If this is not a fall-through branch, emit the branch.
426 if (Succ0MBB != NextBlock)
Chris Lattnera651cf62005-01-17 19:43:36 +0000427 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000428 DAG.getBasicBlock(Succ0MBB)));
Chris Lattner1c08c712005-01-07 07:47:53 +0000429 } else {
430 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
Chris Lattner1c08c712005-01-07 07:47:53 +0000431
432 SDOperand Cond = getValue(I.getCondition());
Chris Lattner1c08c712005-01-07 07:47:53 +0000433 if (Succ1MBB == NextBlock) {
434 // If the condition is false, fall through. This means we should branch
435 // if the condition is true to Succ #0.
Chris Lattnera651cf62005-01-17 19:43:36 +0000436 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000437 Cond, DAG.getBasicBlock(Succ0MBB)));
Chris Lattner1c08c712005-01-07 07:47:53 +0000438 } else if (Succ0MBB == NextBlock) {
439 // If the condition is true, fall through. This means we should branch if
440 // the condition is false to Succ #1. Invert the condition first.
441 SDOperand True = DAG.getConstant(1, Cond.getValueType());
442 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
Chris Lattnera651cf62005-01-17 19:43:36 +0000443 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000444 Cond, DAG.getBasicBlock(Succ1MBB)));
Chris Lattner1c08c712005-01-07 07:47:53 +0000445 } else {
Chris Lattnere7ccd4a2005-04-09 03:30:29 +0000446 std::vector<SDOperand> Ops;
447 Ops.push_back(getRoot());
448 Ops.push_back(Cond);
449 Ops.push_back(DAG.getBasicBlock(Succ0MBB));
450 Ops.push_back(DAG.getBasicBlock(Succ1MBB));
451 DAG.setRoot(DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops));
Chris Lattner1c08c712005-01-07 07:47:53 +0000452 }
453 }
454}
455
Chris Lattnerb9fccc42005-04-02 05:04:50 +0000456void SelectionDAGLowering::visitSub(User &I) {
457 // -0.0 - X --> fneg
458 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
459 if (CFP->isExactlyValue(-0.0)) {
460 SDOperand Op2 = getValue(I.getOperand(1));
461 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
462 return;
463 }
464
465 visitBinary(I, ISD::SUB);
466}
467
Chris Lattner1c08c712005-01-07 07:47:53 +0000468void SelectionDAGLowering::visitBinary(User &I, unsigned Opcode) {
469 SDOperand Op1 = getValue(I.getOperand(0));
470 SDOperand Op2 = getValue(I.getOperand(1));
Chris Lattner2c49f272005-01-19 22:31:21 +0000471
472 if (isa<ShiftInst>(I))
473 Op2 = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), Op2);
474
Chris Lattner1c08c712005-01-07 07:47:53 +0000475 setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
476}
477
478void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode,
479 ISD::CondCode UnsignedOpcode) {
480 SDOperand Op1 = getValue(I.getOperand(0));
481 SDOperand Op2 = getValue(I.getOperand(1));
482 ISD::CondCode Opcode = SignedOpcode;
483 if (I.getOperand(0)->getType()->isUnsigned())
484 Opcode = UnsignedOpcode;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000485 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
Chris Lattner1c08c712005-01-07 07:47:53 +0000486}
487
488void SelectionDAGLowering::visitSelect(User &I) {
489 SDOperand Cond = getValue(I.getOperand(0));
490 SDOperand TrueVal = getValue(I.getOperand(1));
491 SDOperand FalseVal = getValue(I.getOperand(2));
492 setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
493 TrueVal, FalseVal));
494}
495
496void SelectionDAGLowering::visitCast(User &I) {
497 SDOperand N = getValue(I.getOperand(0));
498 MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType());
499 MVT::ValueType DestTy = TLI.getValueType(I.getType());
500
501 if (N.getValueType() == DestTy) {
502 setValue(&I, N); // noop cast.
Chris Lattneref311aa2005-05-09 22:17:13 +0000503 } else if (DestTy == MVT::i1) {
504 // Cast to bool is a comparison against zero, not truncation to zero.
505 SDOperand Zero = isInteger(SrcTy) ? DAG.getConstant(0, N.getValueType()) :
506 DAG.getConstantFP(0.0, N.getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000507 setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE));
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000508 } else if (isInteger(SrcTy)) {
509 if (isInteger(DestTy)) { // Int -> Int cast
510 if (DestTy < SrcTy) // Truncating cast?
511 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N));
512 else if (I.getOperand(0)->getType()->isSigned())
513 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N));
514 else
515 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N));
516 } else { // Int -> FP cast
517 if (I.getOperand(0)->getType()->isSigned())
518 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N));
519 else
520 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N));
521 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000522 } else {
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000523 assert(isFloatingPoint(SrcTy) && "Unknown value type!");
524 if (isFloatingPoint(DestTy)) { // FP -> FP cast
525 if (DestTy < SrcTy) // Rounding cast?
526 setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N));
527 else
528 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N));
529 } else { // FP -> Int cast.
530 if (I.getType()->isSigned())
531 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N));
532 else
533 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N));
534 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000535 }
536}
537
538void SelectionDAGLowering::visitGetElementPtr(User &I) {
539 SDOperand N = getValue(I.getOperand(0));
540 const Type *Ty = I.getOperand(0)->getType();
541 const Type *UIntPtrTy = TD.getIntPtrType();
542
543 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
544 OI != E; ++OI) {
545 Value *Idx = *OI;
546 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
547 unsigned Field = cast<ConstantUInt>(Idx)->getValue();
548 if (Field) {
549 // N = N + Offset
550 uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field];
551 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000552 getIntPtrConstant(Offset));
Chris Lattner1c08c712005-01-07 07:47:53 +0000553 }
554 Ty = StTy->getElementType(Field);
555 } else {
556 Ty = cast<SequentialType>(Ty)->getElementType();
557 if (!isa<Constant>(Idx) || !cast<Constant>(Idx)->isNullValue()) {
558 // N = N + Idx * ElementSize;
559 uint64_t ElementSize = TD.getTypeSize(Ty);
Chris Lattner7cc47772005-01-07 21:56:57 +0000560 SDOperand IdxN = getValue(Idx), Scale = getIntPtrConstant(ElementSize);
561
562 // If the index is smaller or larger than intptr_t, truncate or extend
563 // it.
564 if (IdxN.getValueType() < Scale.getValueType()) {
565 if (Idx->getType()->isSigned())
566 IdxN = DAG.getNode(ISD::SIGN_EXTEND, Scale.getValueType(), IdxN);
567 else
568 IdxN = DAG.getNode(ISD::ZERO_EXTEND, Scale.getValueType(), IdxN);
569 } else if (IdxN.getValueType() > Scale.getValueType())
570 IdxN = DAG.getNode(ISD::TRUNCATE, Scale.getValueType(), IdxN);
571
572 IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
Chris Lattner1c08c712005-01-07 07:47:53 +0000573 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
574 }
575 }
576 }
577 setValue(&I, N);
578}
579
580void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
581 // If this is a fixed sized alloca in the entry block of the function,
582 // allocate it statically on the stack.
583 if (FuncInfo.StaticAllocaMap.count(&I))
584 return; // getValue will auto-populate this.
585
586 const Type *Ty = I.getAllocatedType();
587 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
588 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
589
590 SDOperand AllocSize = getValue(I.getArraySize());
Chris Lattner68cd65e2005-01-22 23:04:37 +0000591 MVT::ValueType IntPtr = TLI.getPointerTy();
592 if (IntPtr < AllocSize.getValueType())
593 AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
594 else if (IntPtr > AllocSize.getValueType())
595 AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
Chris Lattner1c08c712005-01-07 07:47:53 +0000596
Chris Lattner68cd65e2005-01-22 23:04:37 +0000597 AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
Chris Lattner1c08c712005-01-07 07:47:53 +0000598 getIntPtrConstant(TySize));
599
600 // Handle alignment. If the requested alignment is less than or equal to the
601 // stack alignment, ignore it and round the size of the allocation up to the
602 // stack alignment size. If the size is greater than the stack alignment, we
603 // note this in the DYNAMIC_STACKALLOC node.
604 unsigned StackAlign =
605 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
606 if (Align <= StackAlign) {
607 Align = 0;
608 // Add SA-1 to the size.
609 AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
610 getIntPtrConstant(StackAlign-1));
611 // Mask out the low bits for alignment purposes.
612 AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
613 getIntPtrConstant(~(uint64_t)(StackAlign-1)));
614 }
615
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000616 std::vector<MVT::ValueType> VTs;
617 VTs.push_back(AllocSize.getValueType());
618 VTs.push_back(MVT::Other);
619 std::vector<SDOperand> Ops;
620 Ops.push_back(getRoot());
621 Ops.push_back(AllocSize);
622 Ops.push_back(getIntPtrConstant(Align));
623 SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
Chris Lattner1c08c712005-01-07 07:47:53 +0000624 DAG.setRoot(setValue(&I, DSA).getValue(1));
625
626 // Inform the Frame Information that we have just allocated a variable-sized
627 // object.
628 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
629}
630
631
632void SelectionDAGLowering::visitLoad(LoadInst &I) {
633 SDOperand Ptr = getValue(I.getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +0000634
Chris Lattnerd3948112005-01-17 22:19:26 +0000635 SDOperand Root;
636 if (I.isVolatile())
637 Root = getRoot();
638 else {
639 // Do not serialize non-volatile loads against each other.
640 Root = DAG.getRoot();
641 }
642
Chris Lattner369e6db2005-05-09 04:08:33 +0000643 SDOperand L = DAG.getLoad(TLI.getValueType(I.getType()), Root, Ptr,
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000644 DAG.getSrcValue(I.getOperand(0)));
Chris Lattnerd3948112005-01-17 22:19:26 +0000645 setValue(&I, L);
646
647 if (I.isVolatile())
648 DAG.setRoot(L.getValue(1));
649 else
650 PendingLoads.push_back(L.getValue(1));
Chris Lattner1c08c712005-01-07 07:47:53 +0000651}
652
653
654void SelectionDAGLowering::visitStore(StoreInst &I) {
655 Value *SrcV = I.getOperand(0);
656 SDOperand Src = getValue(SrcV);
657 SDOperand Ptr = getValue(I.getOperand(1));
Chris Lattner369e6db2005-05-09 04:08:33 +0000658 DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr,
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000659 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner1c08c712005-01-07 07:47:53 +0000660}
661
662void SelectionDAGLowering::visitCall(CallInst &I) {
Chris Lattner64e14b12005-01-08 22:48:57 +0000663 const char *RenameFn = 0;
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000664 SDOperand Tmp;
Chris Lattner1c08c712005-01-07 07:47:53 +0000665 if (Function *F = I.getCalledFunction())
Chris Lattnerc0f18152005-04-02 05:26:53 +0000666 if (F->isExternal())
667 switch (F->getIntrinsicID()) {
668 case 0: // Not an LLVM intrinsic.
669 if (F->getName() == "fabs" || F->getName() == "fabsf") {
670 if (I.getNumOperands() == 2 && // Basic sanity checks.
671 I.getOperand(1)->getType()->isFloatingPoint() &&
672 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000673 Tmp = getValue(I.getOperand(1));
Chris Lattnerc0f18152005-04-02 05:26:53 +0000674 setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
675 return;
676 }
677 }
Chris Lattnerf76e7dc2005-04-30 04:43:14 +0000678 else if (F->getName() == "sin" || F->getName() == "sinf") {
679 if (I.getNumOperands() == 2 && // Basic sanity checks.
680 I.getOperand(1)->getType()->isFloatingPoint() &&
681 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000682 Tmp = getValue(I.getOperand(1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +0000683 setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
684 return;
685 }
686 }
687 else if (F->getName() == "cos" || F->getName() == "cosf") {
688 if (I.getNumOperands() == 2 && // Basic sanity checks.
689 I.getOperand(1)->getType()->isFloatingPoint() &&
690 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000691 Tmp = getValue(I.getOperand(1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +0000692 setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
693 return;
694 }
695 }
Chris Lattnerc0f18152005-04-02 05:26:53 +0000696 break;
697 case Intrinsic::vastart: visitVAStart(I); return;
698 case Intrinsic::vaend: visitVAEnd(I); return;
699 case Intrinsic::vacopy: visitVACopy(I); return;
700 case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return;
701 case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return;
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000702
Chris Lattnerc0f18152005-04-02 05:26:53 +0000703 case Intrinsic::setjmp: RenameFn = "setjmp"; break;
704 case Intrinsic::longjmp: RenameFn = "longjmp"; break;
705 case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return;
706 case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return;
707 case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000708
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000709 case Intrinsic::readport:
Chris Lattner1ca85d52005-05-14 13:56:55 +0000710 case Intrinsic::readio: {
711 std::vector<MVT::ValueType> VTs;
712 VTs.push_back(TLI.getValueType(I.getType()));
713 VTs.push_back(MVT::Other);
714 std::vector<SDOperand> Ops;
715 Ops.push_back(getRoot());
716 Ops.push_back(getValue(I.getOperand(1)));
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000717 Tmp = DAG.getNode(F->getIntrinsicID() == Intrinsic::readport ?
Chris Lattner1ca85d52005-05-14 13:56:55 +0000718 ISD::READPORT : ISD::READIO, VTs, Ops);
Jeff Cohen00b168892005-07-27 06:12:32 +0000719
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000720 setValue(&I, Tmp);
721 DAG.setRoot(Tmp.getValue(1));
722 return;
Chris Lattner1ca85d52005-05-14 13:56:55 +0000723 }
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000724 case Intrinsic::writeport:
725 case Intrinsic::writeio:
726 DAG.setRoot(DAG.getNode(F->getIntrinsicID() == Intrinsic::writeport ?
727 ISD::WRITEPORT : ISD::WRITEIO, MVT::Other,
728 getRoot(), getValue(I.getOperand(1)),
729 getValue(I.getOperand(2))));
730 return;
Chris Lattner7ea0ade2005-05-05 17:55:17 +0000731 case Intrinsic::dbg_stoppoint:
732 case Intrinsic::dbg_region_start:
733 case Intrinsic::dbg_region_end:
734 case Intrinsic::dbg_func_start:
735 case Intrinsic::dbg_declare:
736 if (I.getType() != Type::VoidTy)
737 setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType())));
738 return;
739
Chris Lattnerc0f18152005-04-02 05:26:53 +0000740 case Intrinsic::isunordered:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000741 setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)),
742 getValue(I.getOperand(2)), ISD::SETUO));
Chris Lattnerc0f18152005-04-02 05:26:53 +0000743 return;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +0000744
745 case Intrinsic::sqrt:
746 setValue(&I, DAG.getNode(ISD::FSQRT,
747 getValue(I.getOperand(1)).getValueType(),
748 getValue(I.getOperand(1))));
749 return;
750
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000751 case Intrinsic::pcmarker:
752 Tmp = getValue(I.getOperand(1));
753 DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
Chris Lattnerc0f18152005-04-02 05:26:53 +0000754 return;
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000755 case Intrinsic::cttz:
756 setValue(&I, DAG.getNode(ISD::CTTZ,
757 getValue(I.getOperand(1)).getValueType(),
758 getValue(I.getOperand(1))));
759 return;
760 case Intrinsic::ctlz:
761 setValue(&I, DAG.getNode(ISD::CTLZ,
762 getValue(I.getOperand(1)).getValueType(),
763 getValue(I.getOperand(1))));
764 return;
765 case Intrinsic::ctpop:
766 setValue(&I, DAG.getNode(ISD::CTPOP,
767 getValue(I.getOperand(1)).getValueType(),
768 getValue(I.getOperand(1))));
769 return;
Chris Lattnerd0f6c1f2005-05-09 20:22:36 +0000770 default:
771 std::cerr << I;
772 assert(0 && "This intrinsic is not implemented yet!");
773 return;
Chris Lattnerc0f18152005-04-02 05:26:53 +0000774 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000775
Chris Lattner64e14b12005-01-08 22:48:57 +0000776 SDOperand Callee;
777 if (!RenameFn)
778 Callee = getValue(I.getOperand(0));
779 else
780 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Chris Lattner1c08c712005-01-07 07:47:53 +0000781 std::vector<std::pair<SDOperand, const Type*> > Args;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000782
Chris Lattner1c08c712005-01-07 07:47:53 +0000783 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
784 Value *Arg = I.getOperand(i);
785 SDOperand ArgNode = getValue(Arg);
786 Args.push_back(std::make_pair(ArgNode, Arg->getType()));
787 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000788
Nate Begeman8e21e712005-03-26 01:29:23 +0000789 const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType());
790 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000791
Chris Lattnercf5734d2005-01-08 19:26:18 +0000792 std::pair<SDOperand,SDOperand> Result =
Chris Lattner9092fa32005-05-12 19:56:57 +0000793 TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(),
Chris Lattneradf6a962005-05-13 18:50:42 +0000794 I.isTailCall(), Callee, Args, DAG);
Chris Lattner1c08c712005-01-07 07:47:53 +0000795 if (I.getType() != Type::VoidTy)
Chris Lattnercf5734d2005-01-08 19:26:18 +0000796 setValue(&I, Result.first);
797 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000798}
799
800void SelectionDAGLowering::visitMalloc(MallocInst &I) {
801 SDOperand Src = getValue(I.getOperand(0));
802
803 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattner68cd65e2005-01-22 23:04:37 +0000804
805 if (IntPtr < Src.getValueType())
806 Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
807 else if (IntPtr > Src.getValueType())
808 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
Chris Lattner1c08c712005-01-07 07:47:53 +0000809
810 // Scale the source by the type size.
811 uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType());
812 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
813 Src, getIntPtrConstant(ElementSize));
814
815 std::vector<std::pair<SDOperand, const Type*> > Args;
816 Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType()));
Chris Lattnercf5734d2005-01-08 19:26:18 +0000817
818 std::pair<SDOperand,SDOperand> Result =
Chris Lattneradf6a962005-05-13 18:50:42 +0000819 TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true,
Chris Lattnercf5734d2005-01-08 19:26:18 +0000820 DAG.getExternalSymbol("malloc", IntPtr),
821 Args, DAG);
822 setValue(&I, Result.first); // Pointers always fit in registers
823 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000824}
825
826void SelectionDAGLowering::visitFree(FreeInst &I) {
827 std::vector<std::pair<SDOperand, const Type*> > Args;
828 Args.push_back(std::make_pair(getValue(I.getOperand(0)),
829 TLI.getTargetData().getIntPtrType()));
830 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattnercf5734d2005-01-08 19:26:18 +0000831 std::pair<SDOperand,SDOperand> Result =
Chris Lattneradf6a962005-05-13 18:50:42 +0000832 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true,
Chris Lattnercf5734d2005-01-08 19:26:18 +0000833 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
834 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000835}
836
Chris Lattnere64e72b2005-07-05 19:57:53 +0000837SDOperand TargetLowering::LowerVAStart(SDOperand Chain,
838 SDOperand VAListP, Value *VAListV,
839 SelectionDAG &DAG) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000840 // We have no sane default behavior, just emit a useful error message and bail
841 // out.
Chris Lattner39ae3622005-01-09 00:00:49 +0000842 std::cerr << "Variable arguments handling not implemented on this target!\n";
Chris Lattner1c08c712005-01-07 07:47:53 +0000843 abort();
Chris Lattnere64e72b2005-07-05 19:57:53 +0000844 return SDOperand();
Chris Lattner1c08c712005-01-07 07:47:53 +0000845}
846
Chris Lattnere64e72b2005-07-05 19:57:53 +0000847SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV,
Chris Lattner39ae3622005-01-09 00:00:49 +0000848 SelectionDAG &DAG) {
849 // Default to a noop.
850 return Chain;
851}
852
Chris Lattnere64e72b2005-07-05 19:57:53 +0000853SDOperand TargetLowering::LowerVACopy(SDOperand Chain,
854 SDOperand SrcP, Value *SrcV,
855 SDOperand DestP, Value *DestV,
856 SelectionDAG &DAG) {
857 // Default to copying the input list.
858 SDOperand Val = DAG.getLoad(getPointerTy(), Chain,
859 SrcP, DAG.getSrcValue(SrcV));
Andrew Lenharth213e5572005-06-22 21:04:42 +0000860 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Chris Lattnere64e72b2005-07-05 19:57:53 +0000861 Val, DestP, DAG.getSrcValue(DestV));
862 return Result;
Chris Lattner39ae3622005-01-09 00:00:49 +0000863}
864
865std::pair<SDOperand,SDOperand>
Chris Lattnere64e72b2005-07-05 19:57:53 +0000866TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
867 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner39ae3622005-01-09 00:00:49 +0000868 // We have no sane default behavior, just emit a useful error message and bail
869 // out.
870 std::cerr << "Variable arguments handling not implemented on this target!\n";
871 abort();
Misha Brukmand3f03e42005-02-17 21:39:27 +0000872 return std::make_pair(SDOperand(), SDOperand());
Chris Lattner39ae3622005-01-09 00:00:49 +0000873}
874
875
876void SelectionDAGLowering::visitVAStart(CallInst &I) {
Chris Lattnere64e72b2005-07-05 19:57:53 +0000877 DAG.setRoot(TLI.LowerVAStart(getRoot(), getValue(I.getOperand(1)),
878 I.getOperand(1), DAG));
Chris Lattner39ae3622005-01-09 00:00:49 +0000879}
880
881void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
882 std::pair<SDOperand,SDOperand> Result =
Chris Lattnere64e72b2005-07-05 19:57:53 +0000883 TLI.LowerVAArg(getRoot(), getValue(I.getOperand(0)), I.getOperand(0),
Andrew Lenharth558bc882005-06-18 18:34:52 +0000884 I.getType(), DAG);
Chris Lattner39ae3622005-01-09 00:00:49 +0000885 setValue(&I, Result.first);
886 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000887}
888
889void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000890 DAG.setRoot(TLI.LowerVAEnd(getRoot(), getValue(I.getOperand(1)),
Chris Lattnere64e72b2005-07-05 19:57:53 +0000891 I.getOperand(1), DAG));
Chris Lattner1c08c712005-01-07 07:47:53 +0000892}
893
894void SelectionDAGLowering::visitVACopy(CallInst &I) {
Chris Lattnere64e72b2005-07-05 19:57:53 +0000895 SDOperand Result =
896 TLI.LowerVACopy(getRoot(), getValue(I.getOperand(2)), I.getOperand(2),
897 getValue(I.getOperand(1)), I.getOperand(1), DAG);
898 DAG.setRoot(Result);
Chris Lattner1c08c712005-01-07 07:47:53 +0000899}
900
Chris Lattner39ae3622005-01-09 00:00:49 +0000901
902// It is always conservatively correct for llvm.returnaddress and
903// llvm.frameaddress to return 0.
904std::pair<SDOperand, SDOperand>
905TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain,
906 unsigned Depth, SelectionDAG &DAG) {
907 return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain);
Chris Lattner1c08c712005-01-07 07:47:53 +0000908}
909
Chris Lattner50381b62005-05-14 05:50:48 +0000910SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner171453a2005-01-16 07:28:41 +0000911 assert(0 && "LowerOperation not implemented for this target!");
912 abort();
Misha Brukmand3f03e42005-02-17 21:39:27 +0000913 return SDOperand();
Chris Lattner171453a2005-01-16 07:28:41 +0000914}
915
Chris Lattner39ae3622005-01-09 00:00:49 +0000916void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) {
917 unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue();
918 std::pair<SDOperand,SDOperand> Result =
Chris Lattnera651cf62005-01-17 19:43:36 +0000919 TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG);
Chris Lattner39ae3622005-01-09 00:00:49 +0000920 setValue(&I, Result.first);
921 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000922}
923
Chris Lattner7041ee32005-01-11 05:56:49 +0000924void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
925 std::vector<SDOperand> Ops;
Chris Lattnera651cf62005-01-17 19:43:36 +0000926 Ops.push_back(getRoot());
Chris Lattner7041ee32005-01-11 05:56:49 +0000927 Ops.push_back(getValue(I.getOperand(1)));
928 Ops.push_back(getValue(I.getOperand(2)));
929 Ops.push_back(getValue(I.getOperand(3)));
930 Ops.push_back(getValue(I.getOperand(4)));
931 DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops));
Chris Lattner1c08c712005-01-07 07:47:53 +0000932}
933
Chris Lattner7041ee32005-01-11 05:56:49 +0000934//===----------------------------------------------------------------------===//
935// SelectionDAGISel code
936//===----------------------------------------------------------------------===//
Chris Lattner1c08c712005-01-07 07:47:53 +0000937
938unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
939 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
940}
941
942
943
944bool SelectionDAGISel::runOnFunction(Function &Fn) {
945 MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
946 RegMap = MF.getSSARegMap();
947 DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n");
948
949 FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
950
951 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
952 SelectBasicBlock(I, MF, FuncInfo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000953
Chris Lattner1c08c712005-01-07 07:47:53 +0000954 return true;
955}
956
957
Chris Lattnerddb870b2005-01-13 17:59:43 +0000958SDOperand SelectionDAGISel::
959CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000960 SelectionDAG &DAG = SDL.DAG;
Chris Lattnerf1fdaca2005-01-11 22:03:46 +0000961 SDOperand Op = SDL.getValue(V);
Chris Lattner18c2f132005-01-13 20:50:02 +0000962 assert((Op.getOpcode() != ISD::CopyFromReg ||
963 cast<RegSDNode>(Op)->getReg() != Reg) &&
964 "Copy from a reg to the same reg!");
Chris Lattnera651cf62005-01-17 19:43:36 +0000965 return DAG.getCopyToReg(SDL.getRoot(), Op, Reg);
Chris Lattner1c08c712005-01-07 07:47:53 +0000966}
967
Chris Lattner0afa8e32005-01-17 17:55:19 +0000968/// IsOnlyUsedInOneBasicBlock - If the specified argument is only used in a
969/// single basic block, return that block. Otherwise, return a null pointer.
970static BasicBlock *IsOnlyUsedInOneBasicBlock(Argument *A) {
971 if (A->use_empty()) return 0;
972 BasicBlock *BB = cast<Instruction>(A->use_back())->getParent();
973 for (Argument::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E;
974 ++UI)
975 if (isa<PHINode>(*UI) || cast<Instruction>(*UI)->getParent() != BB)
976 return 0; // Disagreement among the users?
Chris Lattneraa781b32005-02-17 19:40:32 +0000977
978 // Okay, there is a single BB user. Only permit this optimization if this is
979 // the entry block, otherwise, we might sink argument loads into loops and
980 // stuff. Later, when we have global instruction selection, this won't be an
981 // issue clearly.
982 if (BB == BB->getParent()->begin())
983 return BB;
984 return 0;
Chris Lattner0afa8e32005-01-17 17:55:19 +0000985}
986
Chris Lattner068a81e2005-01-17 17:15:02 +0000987void SelectionDAGISel::
988LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
989 std::vector<SDOperand> &UnorderedChains) {
990 // If this is the entry block, emit arguments.
991 Function &F = *BB->getParent();
Chris Lattner0afa8e32005-01-17 17:55:19 +0000992 FunctionLoweringInfo &FuncInfo = SDL.FuncInfo;
Chris Lattner068a81e2005-01-17 17:15:02 +0000993
994 if (BB == &F.front()) {
Chris Lattner0afa8e32005-01-17 17:55:19 +0000995 SDOperand OldRoot = SDL.DAG.getRoot();
996
Chris Lattner068a81e2005-01-17 17:15:02 +0000997 std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
998
Chris Lattner0afa8e32005-01-17 17:55:19 +0000999 // If there were side effects accessing the argument list, do not do
1000 // anything special.
1001 if (OldRoot != SDL.DAG.getRoot()) {
1002 unsigned a = 0;
Chris Lattnera33ef482005-03-30 01:10:47 +00001003 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1004 AI != E; ++AI,++a)
Chris Lattner0afa8e32005-01-17 17:55:19 +00001005 if (!AI->use_empty()) {
1006 SDL.setValue(AI, Args[a]);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001007 SDOperand Copy =
Chris Lattner0afa8e32005-01-17 17:55:19 +00001008 CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]);
1009 UnorderedChains.push_back(Copy);
1010 }
1011 } else {
1012 // Otherwise, if any argument is only accessed in a single basic block,
1013 // emit that argument only to that basic block.
1014 unsigned a = 0;
Chris Lattnera33ef482005-03-30 01:10:47 +00001015 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1016 AI != E; ++AI,++a)
Chris Lattner0afa8e32005-01-17 17:55:19 +00001017 if (!AI->use_empty()) {
1018 if (BasicBlock *BBU = IsOnlyUsedInOneBasicBlock(AI)) {
1019 FuncInfo.BlockLocalArguments.insert(std::make_pair(BBU,
1020 std::make_pair(AI, a)));
1021 } else {
1022 SDL.setValue(AI, Args[a]);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001023 SDOperand Copy =
Chris Lattner0afa8e32005-01-17 17:55:19 +00001024 CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]);
1025 UnorderedChains.push_back(Copy);
1026 }
1027 }
1028 }
Chris Lattner405ef9e2005-05-13 07:33:32 +00001029
1030 EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction());
Chris Lattner0afa8e32005-01-17 17:55:19 +00001031 }
Chris Lattner068a81e2005-01-17 17:15:02 +00001032
Chris Lattner0afa8e32005-01-17 17:55:19 +00001033 // See if there are any block-local arguments that need to be emitted in this
1034 // block.
1035
1036 if (!FuncInfo.BlockLocalArguments.empty()) {
1037 std::multimap<BasicBlock*, std::pair<Argument*, unsigned> >::iterator BLAI =
1038 FuncInfo.BlockLocalArguments.lower_bound(BB);
1039 if (BLAI != FuncInfo.BlockLocalArguments.end() && BLAI->first == BB) {
1040 // Lower the arguments into this block.
1041 std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001042
Chris Lattner0afa8e32005-01-17 17:55:19 +00001043 // Set up the value mapping for the local arguments.
1044 for (; BLAI != FuncInfo.BlockLocalArguments.end() && BLAI->first == BB;
1045 ++BLAI)
1046 SDL.setValue(BLAI->second.first, Args[BLAI->second.second]);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001047
Chris Lattner0afa8e32005-01-17 17:55:19 +00001048 // Any dead arguments will just be ignored here.
1049 }
Chris Lattner068a81e2005-01-17 17:15:02 +00001050 }
1051}
1052
1053
Chris Lattner1c08c712005-01-07 07:47:53 +00001054void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
1055 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
1056 FunctionLoweringInfo &FuncInfo) {
1057 SelectionDAGLowering SDL(DAG, TLI, FuncInfo);
Chris Lattnerddb870b2005-01-13 17:59:43 +00001058
1059 std::vector<SDOperand> UnorderedChains;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001060
Chris Lattner068a81e2005-01-17 17:15:02 +00001061 // Lower any arguments needed in this block.
1062 LowerArguments(LLVMBB, SDL, UnorderedChains);
Chris Lattner1c08c712005-01-07 07:47:53 +00001063
1064 BB = FuncInfo.MBBMap[LLVMBB];
1065 SDL.setCurrentBasicBlock(BB);
1066
1067 // Lower all of the non-terminator instructions.
1068 for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
1069 I != E; ++I)
1070 SDL.visit(*I);
1071
1072 // Ensure that all instructions which are used outside of their defining
1073 // blocks are available as virtual registers.
1074 for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
Chris Lattnerf1fdaca2005-01-11 22:03:46 +00001075 if (!I->use_empty() && !isa<PHINode>(I)) {
Chris Lattneree749d72005-01-09 01:16:24 +00001076 std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
Chris Lattner1c08c712005-01-07 07:47:53 +00001077 if (VMI != FuncInfo.ValueMap.end())
Chris Lattnerddb870b2005-01-13 17:59:43 +00001078 UnorderedChains.push_back(
1079 CopyValueToVirtualRegister(SDL, I, VMI->second));
Chris Lattner1c08c712005-01-07 07:47:53 +00001080 }
1081
1082 // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
1083 // ensure constants are generated when needed. Remember the virtual registers
1084 // that need to be added to the Machine PHI nodes as input. We cannot just
1085 // directly add them, because expansion might result in multiple MBB's for one
1086 // BB. As such, the start of the BB might correspond to a different MBB than
1087 // the end.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001088 //
Chris Lattner1c08c712005-01-07 07:47:53 +00001089
1090 // Emit constants only once even if used by multiple PHI nodes.
1091 std::map<Constant*, unsigned> ConstantsOut;
1092
1093 // Check successor nodes PHI nodes that expect a constant to be available from
1094 // this block.
1095 TerminatorInst *TI = LLVMBB->getTerminator();
1096 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1097 BasicBlock *SuccBB = TI->getSuccessor(succ);
1098 MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin();
1099 PHINode *PN;
1100
1101 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1102 // nodes and Machine PHI nodes, but the incoming operands have not been
1103 // emitted yet.
1104 for (BasicBlock::iterator I = SuccBB->begin();
Chris Lattnerf44fd882005-01-07 21:34:19 +00001105 (PN = dyn_cast<PHINode>(I)); ++I)
1106 if (!PN->use_empty()) {
1107 unsigned Reg;
1108 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1109 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
1110 unsigned &RegOut = ConstantsOut[C];
1111 if (RegOut == 0) {
1112 RegOut = FuncInfo.CreateRegForValue(C);
Chris Lattnerddb870b2005-01-13 17:59:43 +00001113 UnorderedChains.push_back(
1114 CopyValueToVirtualRegister(SDL, C, RegOut));
Chris Lattnerf44fd882005-01-07 21:34:19 +00001115 }
1116 Reg = RegOut;
1117 } else {
1118 Reg = FuncInfo.ValueMap[PHIOp];
Chris Lattneree749d72005-01-09 01:16:24 +00001119 if (Reg == 0) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001120 assert(isa<AllocaInst>(PHIOp) &&
Chris Lattneree749d72005-01-09 01:16:24 +00001121 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
1122 "Didn't codegen value into a register!??");
1123 Reg = FuncInfo.CreateRegForValue(PHIOp);
Chris Lattnerddb870b2005-01-13 17:59:43 +00001124 UnorderedChains.push_back(
1125 CopyValueToVirtualRegister(SDL, PHIOp, Reg));
Chris Lattneree749d72005-01-09 01:16:24 +00001126 }
Chris Lattner1c08c712005-01-07 07:47:53 +00001127 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001128
Chris Lattnerf44fd882005-01-07 21:34:19 +00001129 // Remember that this register needs to added to the machine PHI node as
1130 // the input for this MBB.
1131 unsigned NumElements =
1132 TLI.getNumElements(TLI.getValueType(PN->getType()));
1133 for (unsigned i = 0, e = NumElements; i != e; ++i)
1134 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Chris Lattner1c08c712005-01-07 07:47:53 +00001135 }
Chris Lattner1c08c712005-01-07 07:47:53 +00001136 }
1137 ConstantsOut.clear();
1138
Chris Lattnerddb870b2005-01-13 17:59:43 +00001139 // Turn all of the unordered chains into one factored node.
Chris Lattner5a6c6d92005-01-13 19:53:14 +00001140 if (!UnorderedChains.empty()) {
Chris Lattnerd3948112005-01-17 22:19:26 +00001141 UnorderedChains.push_back(SDL.getRoot());
Chris Lattnerddb870b2005-01-13 17:59:43 +00001142 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains));
1143 }
1144
Chris Lattner1c08c712005-01-07 07:47:53 +00001145 // Lower the terminator after the copies are emitted.
1146 SDL.visit(*LLVMBB->getTerminator());
Chris Lattnera651cf62005-01-17 19:43:36 +00001147
1148 // Make sure the root of the DAG is up-to-date.
1149 DAG.setRoot(SDL.getRoot());
Chris Lattner1c08c712005-01-07 07:47:53 +00001150}
1151
1152void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
1153 FunctionLoweringInfo &FuncInfo) {
Chris Lattnerac9dc082005-01-23 04:36:26 +00001154 SelectionDAG DAG(TLI, MF);
Chris Lattner1c08c712005-01-07 07:47:53 +00001155 CurDAG = &DAG;
1156 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
1157
1158 // First step, lower LLVM code to some DAG. This DAG may use operations and
1159 // types that are not supported by the target.
1160 BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
1161
1162 DEBUG(std::cerr << "Lowered selection DAG:\n");
1163 DEBUG(DAG.dump());
1164
1165 // Second step, hack on the DAG until it only uses operations and types that
1166 // the target supports.
Chris Lattnerac9dc082005-01-23 04:36:26 +00001167 DAG.Legalize();
Chris Lattner1c08c712005-01-07 07:47:53 +00001168
1169 DEBUG(std::cerr << "Legalized selection DAG:\n");
1170 DEBUG(DAG.dump());
1171
Chris Lattnera33ef482005-03-30 01:10:47 +00001172 // Third, instruction select all of the operations to machine code, adding the
1173 // code to the MachineBasicBlock.
Chris Lattner1c08c712005-01-07 07:47:53 +00001174 InstructionSelectBasicBlock(DAG);
1175
Chris Lattner7944d9d2005-01-12 03:41:21 +00001176 if (ViewDAGs) DAG.viewGraph();
1177
Chris Lattner1c08c712005-01-07 07:47:53 +00001178 DEBUG(std::cerr << "Selected machine code:\n");
1179 DEBUG(BB->dump());
1180
Chris Lattnera33ef482005-03-30 01:10:47 +00001181 // Next, now that we know what the last MBB the LLVM BB expanded is, update
Chris Lattner1c08c712005-01-07 07:47:53 +00001182 // PHI nodes in successors.
1183 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
1184 MachineInstr *PHI = PHINodesToUpdate[i].first;
1185 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
1186 "This is not a machine PHI node that we are updating!");
1187 PHI->addRegOperand(PHINodesToUpdate[i].second);
1188 PHI->addMachineBasicBlockOperand(BB);
1189 }
Chris Lattnera33ef482005-03-30 01:10:47 +00001190
1191 // Finally, add the CFG edges from the last selected MBB to the successor
1192 // MBBs.
1193 TerminatorInst *TI = LLVMBB->getTerminator();
1194 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1195 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)];
1196 BB->addSuccessor(Succ0MBB);
1197 }
Chris Lattner1c08c712005-01-07 07:47:53 +00001198}