blob: 18ea84cd6664ef145ae7569639e0b8547bdbe13a [file] [log] [blame]
Misha Brukmanca9309f2004-08-11 23:42:15 +00001//===-- PPC64ISelSimple.cpp - A simple instruction selector for PowerPC ---===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9
10#define DEBUG_TYPE "isel"
11#include "PowerPC.h"
12#include "PowerPCInstrBuilder.h"
13#include "PowerPCInstrInfo.h"
14#include "PPC64TargetMachine.h"
15#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/Pass.h"
20#include "llvm/CodeGen/IntrinsicLowering.h"
21#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/MRegisterInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/InstVisitor.h"
29#include "Support/Debug.h"
30#include "Support/Statistic.h"
31#include <vector>
32using namespace llvm;
33
34namespace {
35 Statistic<> GEPFolds("ppc64-codegen", "Number of GEPs folded");
36
37 /// TypeClass - Used by the PowerPC backend to group LLVM types by their basic
38 /// PPC Representation.
39 ///
40 enum TypeClass {
41 cByte, cShort, cInt, cFP32, cFP64, cLong
42 };
43}
44
45/// getClass - Turn a primitive type into a "class" number which is based on the
46/// size of the type, and whether or not it is floating point.
47///
48static inline TypeClass getClass(const Type *Ty) {
49 switch (Ty->getTypeID()) {
50 case Type::SByteTyID:
51 case Type::UByteTyID: return cByte; // Byte operands are class #0
52 case Type::ShortTyID:
53 case Type::UShortTyID: return cShort; // Short operands are class #1
54 case Type::IntTyID:
Misha Brukmancc6b01b2004-08-12 02:53:01 +000055 case Type::UIntTyID: return cInt; // Ints are class #2
Misha Brukmanca9309f2004-08-11 23:42:15 +000056
57 case Type::FloatTyID: return cFP32; // Single float is #3
58 case Type::DoubleTyID: return cFP64; // Double Point is #4
59
Misha Brukmancc6b01b2004-08-12 02:53:01 +000060 case Type::PointerTyID:
Misha Brukmanca9309f2004-08-11 23:42:15 +000061 case Type::LongTyID:
Misha Brukmancc6b01b2004-08-12 02:53:01 +000062 case Type::ULongTyID: return cLong; // Longs and pointers are class #5
Misha Brukmanca9309f2004-08-11 23:42:15 +000063 default:
64 assert(0 && "Invalid type to getClass!");
65 return cByte; // not reached
66 }
67}
68
69// getClassB - Just like getClass, but treat boolean values as ints.
70static inline TypeClass getClassB(const Type *Ty) {
71 if (Ty == Type::BoolTy) return cInt;
72 return getClass(Ty);
73}
74
75namespace {
76 struct ISel : public FunctionPass, InstVisitor<ISel> {
77 PPC64TargetMachine &TM;
78 MachineFunction *F; // The function we are compiling into
79 MachineBasicBlock *BB; // The current MBB we are compiling
80 int VarArgsFrameIndex; // FrameIndex for start of varargs area
81
82 std::map<Value*, unsigned> RegMap; // Mapping between Values and SSA Regs
83
84 // External functions used in the Module
85 Function *fmodfFn, *fmodFn, *__cmpdi2Fn, *__moddi3Fn, *__divdi3Fn,
86 *__umoddi3Fn, *__udivdi3Fn, *__fixsfdiFn, *__fixdfdiFn, *__fixunssfdiFn,
87 *__fixunsdfdiFn, *__floatdisfFn, *__floatdidfFn, *mallocFn, *freeFn;
88
89 // MBBMap - Mapping between LLVM BB -> Machine BB
90 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
91
92 // AllocaMap - Mapping from fixed sized alloca instructions to the
93 // FrameIndex for the alloca.
94 std::map<AllocaInst*, unsigned> AllocaMap;
95
Misha Brukmanca9309f2004-08-11 23:42:15 +000096 ISel(TargetMachine &tm) : TM(reinterpret_cast<PPC64TargetMachine&>(tm)),
97 F(0), BB(0) {}
98
99 bool doInitialization(Module &M) {
100 // Add external functions that we may call
101 Type *i = Type::IntTy;
102 Type *d = Type::DoubleTy;
103 Type *f = Type::FloatTy;
104 Type *l = Type::LongTy;
105 Type *ul = Type::ULongTy;
106 Type *voidPtr = PointerType::get(Type::SByteTy);
107 // float fmodf(float, float);
108 fmodfFn = M.getOrInsertFunction("fmodf", f, f, f, 0);
109 // double fmod(double, double);
110 fmodFn = M.getOrInsertFunction("fmod", d, d, d, 0);
111 // int __cmpdi2(long, long);
112 __cmpdi2Fn = M.getOrInsertFunction("__cmpdi2", i, l, l, 0);
113 // long __moddi3(long, long);
114 __moddi3Fn = M.getOrInsertFunction("__moddi3", l, l, l, 0);
115 // long __divdi3(long, long);
116 __divdi3Fn = M.getOrInsertFunction("__divdi3", l, l, l, 0);
117 // unsigned long __umoddi3(unsigned long, unsigned long);
118 __umoddi3Fn = M.getOrInsertFunction("__umoddi3", ul, ul, ul, 0);
119 // unsigned long __udivdi3(unsigned long, unsigned long);
120 __udivdi3Fn = M.getOrInsertFunction("__udivdi3", ul, ul, ul, 0);
121 // long __fixsfdi(float)
122 __fixsfdiFn = M.getOrInsertFunction("__fixsfdi", l, f, 0);
123 // long __fixdfdi(double)
124 __fixdfdiFn = M.getOrInsertFunction("__fixdfdi", l, d, 0);
125 // unsigned long __fixunssfdi(float)
126 __fixunssfdiFn = M.getOrInsertFunction("__fixunssfdi", ul, f, 0);
127 // unsigned long __fixunsdfdi(double)
128 __fixunsdfdiFn = M.getOrInsertFunction("__fixunsdfdi", ul, d, 0);
129 // float __floatdisf(long)
130 __floatdisfFn = M.getOrInsertFunction("__floatdisf", f, l, 0);
131 // double __floatdidf(long)
132 __floatdidfFn = M.getOrInsertFunction("__floatdidf", d, l, 0);
133 // void* malloc(size_t)
134 mallocFn = M.getOrInsertFunction("malloc", voidPtr, Type::UIntTy, 0);
135 // void free(void*)
136 freeFn = M.getOrInsertFunction("free", Type::VoidTy, voidPtr, 0);
137 return false;
138 }
139
140 /// runOnFunction - Top level implementation of instruction selection for
141 /// the entire function.
142 ///
143 bool runOnFunction(Function &Fn) {
144 // First pass over the function, lower any unknown intrinsic functions
145 // with the IntrinsicLowering class.
146 LowerUnknownIntrinsicFunctionCalls(Fn);
147
148 F = &MachineFunction::construct(&Fn, TM);
149
150 // Create all of the machine basic blocks for the function...
151 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
152 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
153
154 BB = &F->front();
155
Misha Brukmanca9309f2004-08-11 23:42:15 +0000156 // Copy incoming arguments off of the stack...
157 LoadArgumentsToVirtualRegs(Fn);
158
159 // Instruction select everything except PHI nodes
160 visit(Fn);
161
162 // Select the PHI nodes
163 SelectPHINodes();
164
165 RegMap.clear();
166 MBBMap.clear();
167 AllocaMap.clear();
168 F = 0;
169 // We always build a machine code representation for the function
170 return true;
171 }
172
173 virtual const char *getPassName() const {
174 return "PowerPC Simple Instruction Selection";
175 }
176
177 /// visitBasicBlock - This method is called when we are visiting a new basic
178 /// block. This simply creates a new MachineBasicBlock to emit code into
179 /// and adds it to the current MachineFunction. Subsequent visit* for
180 /// instructions will be invoked for all instructions in the basic block.
181 ///
182 void visitBasicBlock(BasicBlock &LLVM_BB) {
183 BB = MBBMap[&LLVM_BB];
184 }
185
186 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
187 /// function, lowering any calls to unknown intrinsic functions into the
188 /// equivalent LLVM code.
189 ///
190 void LowerUnknownIntrinsicFunctionCalls(Function &F);
191
192 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
193 /// from the stack into virtual registers.
194 ///
195 void LoadArgumentsToVirtualRegs(Function &F);
196
197 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
198 /// because we have to generate our sources into the source basic blocks,
199 /// not the current one.
200 ///
201 void SelectPHINodes();
202
203 // Visitation methods for various instructions. These methods simply emit
204 // fixed PowerPC code for each instruction.
205
206 // Control flow operators
207 void visitReturnInst(ReturnInst &RI);
208 void visitBranchInst(BranchInst &BI);
209
210 struct ValueRecord {
211 Value *Val;
212 unsigned Reg;
213 const Type *Ty;
214 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
215 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
216 };
217
218 // This struct is for recording the necessary operations to emit the GEP
219 struct CollapsedGepOp {
220 bool isMul;
221 Value *index;
222 ConstantSInt *size;
223 CollapsedGepOp(bool mul, Value *i, ConstantSInt *s) :
224 isMul(mul), index(i), size(s) {}
225 };
226
227 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
228 const std::vector<ValueRecord> &Args, bool isVarArg);
229 void visitCallInst(CallInst &I);
230 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
231
232 // Arithmetic operators
233 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
234 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
235 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
236 void visitMul(BinaryOperator &B);
237
238 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
239 void visitRem(BinaryOperator &B) { visitDivRem(B); }
240 void visitDivRem(BinaryOperator &B);
241
242 // Bitwise operators
243 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
244 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
245 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
246
247 // Comparison operators...
248 void visitSetCondInst(SetCondInst &I);
249 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
250 MachineBasicBlock *MBB,
251 MachineBasicBlock::iterator MBBI);
252 void visitSelectInst(SelectInst &SI);
253
254
255 // Memory Instructions
256 void visitLoadInst(LoadInst &I);
257 void visitStoreInst(StoreInst &I);
258 void visitGetElementPtrInst(GetElementPtrInst &I);
259 void visitAllocaInst(AllocaInst &I);
260 void visitMallocInst(MallocInst &I);
261 void visitFreeInst(FreeInst &I);
262
263 // Other operators
264 void visitShiftInst(ShiftInst &I);
265 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
266 void visitCastInst(CastInst &I);
267 void visitVANextInst(VANextInst &I);
268 void visitVAArgInst(VAArgInst &I);
269
270 void visitInstruction(Instruction &I) {
271 std::cerr << "Cannot instruction select: " << I;
272 abort();
273 }
274
275 /// promote32 - Make a value 32-bits wide, and put it somewhere.
276 ///
277 void promote32(unsigned targetReg, const ValueRecord &VR);
278
279 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
280 /// constant expression GEP support.
281 ///
282 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
283 Value *Src, User::op_iterator IdxBegin,
284 User::op_iterator IdxEnd, unsigned TargetReg,
285 bool CollapseRemainder, ConstantSInt **Remainder,
286 unsigned *PendingAddReg);
287
288 /// emitCastOperation - Common code shared between visitCastInst and
289 /// constant expression cast support.
290 ///
291 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
292 Value *Src, const Type *DestTy, unsigned TargetReg);
293
294 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
295 /// and constant expression support.
296 ///
297 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
298 MachineBasicBlock::iterator IP,
299 Value *Op0, Value *Op1,
300 unsigned OperatorClass, unsigned TargetReg);
301
302 /// emitBinaryFPOperation - This method handles emission of floating point
303 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
304 void emitBinaryFPOperation(MachineBasicBlock *BB,
305 MachineBasicBlock::iterator IP,
306 Value *Op0, Value *Op1,
307 unsigned OperatorClass, unsigned TargetReg);
308
309 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
310 Value *Op0, Value *Op1, unsigned TargetReg);
311
312 void doMultiply(MachineBasicBlock *MBB,
313 MachineBasicBlock::iterator IP,
314 unsigned DestReg, Value *Op0, Value *Op1);
315
316 /// doMultiplyConst - This method will multiply the value in Op0Reg by the
317 /// value of the ContantInt *CI
318 void doMultiplyConst(MachineBasicBlock *MBB,
319 MachineBasicBlock::iterator IP,
320 unsigned DestReg, Value *Op0, ConstantInt *CI);
321
322 void emitDivRemOperation(MachineBasicBlock *BB,
323 MachineBasicBlock::iterator IP,
324 Value *Op0, Value *Op1, bool isDiv,
325 unsigned TargetReg);
326
327 /// emitSetCCOperation - Common code shared between visitSetCondInst and
328 /// constant expression support.
329 ///
330 void emitSetCCOperation(MachineBasicBlock *BB,
331 MachineBasicBlock::iterator IP,
332 Value *Op0, Value *Op1, unsigned Opcode,
333 unsigned TargetReg);
334
335 /// emitShiftOperation - Common code shared between visitShiftInst and
336 /// constant expression support.
337 ///
338 void emitShiftOperation(MachineBasicBlock *MBB,
339 MachineBasicBlock::iterator IP,
340 Value *Op, Value *ShiftAmount, bool isLeftShift,
341 const Type *ResultTy, unsigned DestReg);
342
343 /// emitSelectOperation - Common code shared between visitSelectInst and the
344 /// constant expression support.
345 ///
346 void emitSelectOperation(MachineBasicBlock *MBB,
347 MachineBasicBlock::iterator IP,
348 Value *Cond, Value *TrueVal, Value *FalseVal,
349 unsigned DestReg);
350
Misha Brukmanca9309f2004-08-11 23:42:15 +0000351 /// copyConstantToRegister - Output the instructions required to put the
352 /// specified constant into the specified register.
353 ///
354 void copyConstantToRegister(MachineBasicBlock *MBB,
355 MachineBasicBlock::iterator MBBI,
356 Constant *C, unsigned Reg);
357
358 void emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
359 unsigned LHS, unsigned RHS);
360
361 /// makeAnotherReg - This method returns the next register number we haven't
362 /// yet used.
363 ///
364 unsigned makeAnotherReg(const Type *Ty) {
Misha Brukmanadde6992004-08-17 04:57:37 +0000365 assert(dynamic_cast<const PPC64RegisterInfo*>(TM.getRegisterInfo()) &&
Misha Brukmanca9309f2004-08-11 23:42:15 +0000366 "Current target doesn't have PPC reg info??");
Misha Brukmanadde6992004-08-17 04:57:37 +0000367 const PPC64RegisterInfo *PPCRI =
368 static_cast<const PPC64RegisterInfo*>(TM.getRegisterInfo());
Misha Brukmanca9309f2004-08-11 23:42:15 +0000369 // Add the mapping of regnumber => reg class to MachineFunction
370 const TargetRegisterClass *RC = PPCRI->getRegClassForType(Ty);
371 return F->getSSARegMap()->createVirtualRegister(RC);
372 }
373
374 /// getReg - This method turns an LLVM value into a register number.
375 ///
376 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
377 unsigned getReg(Value *V) {
378 // Just append to the end of the current bb.
379 MachineBasicBlock::iterator It = BB->end();
380 return getReg(V, BB, It);
381 }
382 unsigned getReg(Value *V, MachineBasicBlock *MBB,
383 MachineBasicBlock::iterator IPt);
384
385 /// canUseAsImmediateForOpcode - This method returns whether a ConstantInt
386 /// is okay to use as an immediate argument to a certain binary operation
387 bool canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Opcode);
388
389 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
390 /// that is to be statically allocated with the initial stack frame
391 /// adjustment.
392 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
393 };
394}
395
396/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
397/// instruction in the entry block, return it. Otherwise, return a null
398/// pointer.
399static AllocaInst *dyn_castFixedAlloca(Value *V) {
400 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
401 BasicBlock *BB = AI->getParent();
402 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
403 return AI;
404 }
405 return 0;
406}
407
408/// getReg - This method turns an LLVM value into a register number.
409///
410unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB,
411 MachineBasicBlock::iterator IPt) {
412 if (Constant *C = dyn_cast<Constant>(V)) {
413 unsigned Reg = makeAnotherReg(V->getType());
414 copyConstantToRegister(MBB, IPt, C, Reg);
415 return Reg;
416 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
417 unsigned Reg = makeAnotherReg(V->getType());
418 unsigned FI = getFixedSizedAllocaFI(AI);
419 addFrameReference(BuildMI(*MBB, IPt, PPC::ADDI, 2, Reg), FI, 0, false);
420 return Reg;
421 }
422
423 unsigned &Reg = RegMap[V];
424 if (Reg == 0) {
425 Reg = makeAnotherReg(V->getType());
426 RegMap[V] = Reg;
427 }
428
429 return Reg;
430}
431
432/// canUseAsImmediateForOpcode - This method returns whether a ConstantInt
433/// is okay to use as an immediate argument to a certain binary operator.
434///
435/// Operator is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for Xor.
436bool ISel::canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Operator) {
437 ConstantSInt *Op1Cs;
438 ConstantUInt *Op1Cu;
439
440 // ADDI, Compare, and non-indexed Load take SIMM
441 bool cond1 = (Operator == 0)
442 && (Op1Cs = dyn_cast<ConstantSInt>(CI))
443 && (Op1Cs->getValue() <= 32767)
444 && (Op1Cs->getValue() >= -32768);
445
446 // SUBI takes -SIMM since it is a mnemonic for ADDI
447 bool cond2 = (Operator == 1)
448 && (Op1Cs = dyn_cast<ConstantSInt>(CI))
449 && (Op1Cs->getValue() <= 32768)
450 && (Op1Cs->getValue() >= -32767);
451
452 // ANDIo, ORI, and XORI take unsigned values
453 bool cond3 = (Operator >= 2)
454 && (Op1Cs = dyn_cast<ConstantSInt>(CI))
455 && (Op1Cs->getValue() >= 0)
456 && (Op1Cs->getValue() <= 32767);
457
458 // ADDI and SUBI take SIMMs, so we have to make sure the UInt would fit
459 bool cond4 = (Operator < 2)
460 && (Op1Cu = dyn_cast<ConstantUInt>(CI))
461 && (Op1Cu->getValue() <= 32767);
462
463 // ANDIo, ORI, and XORI take UIMMs, so they can be larger
464 bool cond5 = (Operator >= 2)
465 && (Op1Cu = dyn_cast<ConstantUInt>(CI))
466 && (Op1Cu->getValue() <= 65535);
467
468 if (cond1 || cond2 || cond3 || cond4 || cond5)
469 return true;
470
471 return false;
472}
473
474/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
475/// that is to be statically allocated with the initial stack frame
476/// adjustment.
477unsigned ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
478 // Already computed this?
479 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
480 if (I != AllocaMap.end() && I->first == AI) return I->second;
481
482 const Type *Ty = AI->getAllocatedType();
483 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
484 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
485 TySize *= CUI->getValue(); // Get total allocated size...
486 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
487
488 // Create a new stack object using the frame manager...
489 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
490 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
491 return FrameIdx;
492}
493
494
Misha Brukmanca9309f2004-08-11 23:42:15 +0000495/// copyConstantToRegister - Output the instructions required to put the
496/// specified constant into the specified register.
497///
498void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
499 MachineBasicBlock::iterator IP,
500 Constant *C, unsigned R) {
501 if (C->getType()->isIntegral()) {
502 unsigned Class = getClassB(C->getType());
503
504 if (Class == cLong) {
505 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
506 uint64_t uval = CUI->getValue();
507 if (uval < (1LL << 32)) {
508 ConstantUInt *CU = ConstantUInt::get(Type::UIntTy, uval);
509 copyConstantToRegister(MBB, IP, CU, R);
510 return;
511 }
512 } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
513 int64_t val = CUI->getValue();
514 if (val < (1LL << 31)) {
515 ConstantUInt *CU = ConstantUInt::get(Type::UIntTy, val);
516 copyConstantToRegister(MBB, IP, CU, R);
517 return;
518 }
519 } else {
520 std::cerr << "Unhandled long constant type!\n";
521 abort();
522 }
523 // Spill long to the constant pool and load it
524 MachineConstantPool *CP = F->getConstantPool();
525 unsigned CPI = CP->getConstantPoolIndex(C);
526 BuildMI(*MBB, IP, PPC::LD, 1, R)
527 .addReg(PPC::R2).addConstantPoolIndex(CPI);
Misha Brukman1c514ec2004-08-19 16:29:25 +0000528 return;
Misha Brukmanca9309f2004-08-11 23:42:15 +0000529 }
530
531 assert(Class <= cInt && "Type not handled yet!");
532
533 // Handle bool
534 if (C->getType() == Type::BoolTy) {
535 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(C == ConstantBool::True);
536 return;
537 }
538
539 // Handle int
540 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
541 unsigned uval = CUI->getValue();
542 if (uval < 32768) {
543 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(uval);
544 } else {
545 unsigned Temp = makeAnotherReg(Type::IntTy);
546 BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(uval >> 16);
547 BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(uval);
548 }
549 return;
550 } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
551 int sval = CSI->getValue();
552 if (sval < 32768 && sval >= -32768) {
553 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(sval);
554 } else {
555 unsigned Temp = makeAnotherReg(Type::IntTy);
556 BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(sval >> 16);
557 BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(sval);
558 }
559 return;
560 }
561 std::cerr << "Unhandled integer constant!\n";
562 abort();
563 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
564 // We need to spill the constant to memory...
565 MachineConstantPool *CP = F->getConstantPool();
566 unsigned CPI = CP->getConstantPoolIndex(CFP);
567 const Type *Ty = CFP->getType();
568 unsigned LoadOpcode = (Ty == Type::FloatTy) ? PPC::LFS : PPC::LFD;
569 BuildMI(*MBB,IP,LoadOpcode,2,R).addConstantPoolIndex(CPI).addReg(PPC::R2);
570 } else if (isa<ConstantPointerNull>(C)) {
571 // Copy zero (null pointer) to the register.
572 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(0);
573 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Misha Brukmancc6b01b2004-08-12 02:53:01 +0000574 static unsigned OpcodeTable[] = {
575 PPC::LBZ, PPC::LHZ, PPC::LWZ, PPC::LFS, PPC::LFD, PPC::LD
576 };
577 unsigned Opcode = OpcodeTable[getClassB(GV->getType())];
578 BuildMI(*MBB, IP, Opcode, 2, R).addGlobalAddress(GV).addReg(PPC::R2);
Misha Brukmanca9309f2004-08-11 23:42:15 +0000579 } else {
580 std::cerr << "Offending constant: " << *C << "\n";
581 assert(0 && "Type not handled yet!");
582 }
583}
584
585/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
586/// the stack into virtual registers.
587void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
588 unsigned ArgOffset = 24;
589 unsigned GPR_remaining = 8;
590 unsigned FPR_remaining = 13;
591 unsigned GPR_idx = 0, FPR_idx = 0;
592 static const unsigned GPR[] = {
593 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
594 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
595 };
596 static const unsigned FPR[] = {
597 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
598 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
599 };
600
601 MachineFrameInfo *MFI = F->getFrameInfo();
602
603 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
604 bool ArgLive = !I->use_empty();
605 unsigned Reg = ArgLive ? getReg(*I) : 0;
606 int FI; // Frame object index
607
608 switch (getClassB(I->getType())) {
609 case cByte:
610 if (ArgLive) {
611 FI = MFI->CreateFixedObject(4, ArgOffset);
612 if (GPR_remaining > 0) {
613 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
614 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
615 .addReg(GPR[GPR_idx]);
616 } else {
617 addFrameReference(BuildMI(BB, PPC::LBZ, 2, Reg), FI);
618 }
619 }
620 break;
621 case cShort:
622 if (ArgLive) {
623 FI = MFI->CreateFixedObject(4, ArgOffset);
624 if (GPR_remaining > 0) {
625 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
626 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
627 .addReg(GPR[GPR_idx]);
628 } else {
629 addFrameReference(BuildMI(BB, PPC::LHZ, 2, Reg), FI);
630 }
631 }
632 break;
633 case cInt:
634 if (ArgLive) {
635 FI = MFI->CreateFixedObject(4, ArgOffset);
636 if (GPR_remaining > 0) {
637 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
638 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
639 .addReg(GPR[GPR_idx]);
640 } else {
641 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg), FI);
642 }
643 }
644 break;
645 case cLong:
646 if (ArgLive) {
647 FI = MFI->CreateFixedObject(8, ArgOffset);
648 if (GPR_remaining > 1) {
649 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
650 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
651 .addReg(GPR[GPR_idx]);
652 } else {
653 addFrameReference(BuildMI(BB, PPC::LD, 2, Reg), FI);
654 }
655 }
656 // longs require 4 additional bytes
657 ArgOffset += 4;
658 break;
659 case cFP32:
660 if (ArgLive) {
661 FI = MFI->CreateFixedObject(4, ArgOffset);
662
663 if (FPR_remaining > 0) {
664 BuildMI(BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
665 BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]);
666 FPR_remaining--;
667 FPR_idx++;
668 } else {
669 addFrameReference(BuildMI(BB, PPC::LFS, 2, Reg), FI);
670 }
671 }
672 break;
673 case cFP64:
674 if (ArgLive) {
675 FI = MFI->CreateFixedObject(8, ArgOffset);
676
677 if (FPR_remaining > 0) {
678 BuildMI(BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
679 BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]);
680 FPR_remaining--;
681 FPR_idx++;
682 } else {
683 addFrameReference(BuildMI(BB, PPC::LFD, 2, Reg), FI);
684 }
685 }
686
687 // doubles require 4 additional bytes and use 2 GPRs of param space
688 ArgOffset += 4;
689 if (GPR_remaining > 0) {
690 GPR_remaining--;
691 GPR_idx++;
692 }
693 break;
694 default:
695 assert(0 && "Unhandled argument type!");
696 }
697 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
698 if (GPR_remaining > 0) {
699 GPR_remaining--; // uses up 2 GPRs
700 GPR_idx++;
701 }
702 }
703
704 // If the function takes variable number of arguments, add a frame offset for
705 // the start of the first vararg value... this is used to expand
706 // llvm.va_start.
707 if (Fn.getFunctionType()->isVarArg())
708 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
709}
710
711
712/// SelectPHINodes - Insert machine code to generate phis. This is tricky
713/// because we have to generate our sources into the source basic blocks, not
714/// the current one.
715///
716void ISel::SelectPHINodes() {
717 const TargetInstrInfo &TII = *TM.getInstrInfo();
718 const Function &LF = *F->getFunction(); // The LLVM function...
719 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
720 const BasicBlock *BB = I;
721 MachineBasicBlock &MBB = *MBBMap[I];
722
723 // Loop over all of the PHI nodes in the LLVM basic block...
724 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
725 for (BasicBlock::const_iterator I = BB->begin();
726 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
727
728 // Create a new machine instr PHI node, and insert it.
729 unsigned PHIReg = getReg(*PN);
730 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
731 PPC::PHI, PN->getNumOperands(), PHIReg);
732
733 // PHIValues - Map of blocks to incoming virtual registers. We use this
734 // so that we only initialize one incoming value for a particular block,
735 // even if the block has multiple entries in the PHI node.
736 //
737 std::map<MachineBasicBlock*, unsigned> PHIValues;
738
739 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
740 MachineBasicBlock *PredMBB = 0;
741 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
742 PE = MBB.pred_end (); PI != PE; ++PI)
743 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
744 PredMBB = *PI;
745 break;
746 }
747 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
748
749 unsigned ValReg;
750 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
751 PHIValues.lower_bound(PredMBB);
752
753 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
754 // We already inserted an initialization of the register for this
755 // predecessor. Recycle it.
756 ValReg = EntryIt->second;
757 } else {
758 // Get the incoming value into a virtual register.
759 //
760 Value *Val = PN->getIncomingValue(i);
761
762 // If this is a constant or GlobalValue, we may have to insert code
763 // into the basic block to compute it into a virtual register.
764 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
765 isa<GlobalValue>(Val)) {
766 // Simple constants get emitted at the end of the basic block,
767 // before any terminator instructions. We "know" that the code to
768 // move a constant into a register will never clobber any flags.
769 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
770 } else {
771 // Because we don't want to clobber any values which might be in
772 // physical registers with the computation of this constant (which
773 // might be arbitrarily complex if it is a constant expression),
774 // just insert the computation at the top of the basic block.
775 MachineBasicBlock::iterator PI = PredMBB->begin();
776
777 // Skip over any PHI nodes though!
778 while (PI != PredMBB->end() && PI->getOpcode() == PPC::PHI)
779 ++PI;
780
781 ValReg = getReg(Val, PredMBB, PI);
782 }
783
784 // Remember that we inserted a value for this PHI for this predecessor
785 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
786 }
787
788 PhiMI->addRegOperand(ValReg);
789 PhiMI->addMachineBasicBlockOperand(PredMBB);
790 }
791
792 // Now that we emitted all of the incoming values for the PHI node, make
793 // sure to reposition the InsertPoint after the PHI that we just added.
794 // This is needed because we might have inserted a constant into this
795 // block, right after the PHI's which is before the old insert point!
796 PHIInsertPoint = PhiMI;
797 ++PHIInsertPoint;
798 }
799 }
800}
801
802
803// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
804// it into the conditional branch or select instruction which is the only user
805// of the cc instruction. This is the case if the conditional branch is the
806// only user of the setcc, and if the setcc is in the same basic block as the
807// conditional branch.
808//
809static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
810 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
811 if (SCI->hasOneUse()) {
812 Instruction *User = cast<Instruction>(SCI->use_back());
813 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
814 SCI->getParent() == User->getParent())
815 return SCI;
816 }
817 return 0;
818}
819
820
821// canFoldGEPIntoLoadOrStore - Return the GEP instruction if we can fold it into
822// the load or store instruction that is the only user of the GEP.
823//
824static GetElementPtrInst *canFoldGEPIntoLoadOrStore(Value *V) {
825 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V))
826 if (GEPI->hasOneUse()) {
827 Instruction *User = cast<Instruction>(GEPI->use_back());
828 if (isa<StoreInst>(User) &&
829 GEPI->getParent() == User->getParent() &&
830 User->getOperand(0) != GEPI &&
831 User->getOperand(1) == GEPI) {
832 ++GEPFolds;
833 return GEPI;
834 }
835 if (isa<LoadInst>(User) &&
836 GEPI->getParent() == User->getParent() &&
837 User->getOperand(0) == GEPI) {
838 ++GEPFolds;
839 return GEPI;
840 }
841 }
842 return 0;
843}
844
845
846// Return a fixed numbering for setcc instructions which does not depend on the
847// order of the opcodes.
848//
849static unsigned getSetCCNumber(unsigned Opcode) {
850 switch (Opcode) {
851 default: assert(0 && "Unknown setcc instruction!");
852 case Instruction::SetEQ: return 0;
853 case Instruction::SetNE: return 1;
854 case Instruction::SetLT: return 2;
855 case Instruction::SetGE: return 3;
856 case Instruction::SetGT: return 4;
857 case Instruction::SetLE: return 5;
858 }
859}
860
861static unsigned getPPCOpcodeForSetCCNumber(unsigned Opcode) {
862 switch (Opcode) {
863 default: assert(0 && "Unknown setcc instruction!");
864 case Instruction::SetEQ: return PPC::BEQ;
865 case Instruction::SetNE: return PPC::BNE;
866 case Instruction::SetLT: return PPC::BLT;
867 case Instruction::SetGE: return PPC::BGE;
868 case Instruction::SetGT: return PPC::BGT;
869 case Instruction::SetLE: return PPC::BLE;
870 }
871}
872
873/// emitUCOM - emits an unordered FP compare.
874void ISel::emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
Misha Brukman1c514ec2004-08-19 16:29:25 +0000875 unsigned LHS, unsigned RHS) {
Misha Brukmanca9309f2004-08-11 23:42:15 +0000876 BuildMI(*MBB, IP, PPC::FCMPU, 2, PPC::CR0).addReg(LHS).addReg(RHS);
877}
878
879/// EmitComparison - emits a comparison of the two operands, returning the
880/// extended setcc code to use. The result is in CR0.
881///
882unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
883 MachineBasicBlock *MBB,
884 MachineBasicBlock::iterator IP) {
885 // The arguments are already supposed to be of the same type.
886 const Type *CompTy = Op0->getType();
887 unsigned Class = getClassB(CompTy);
888 unsigned Op0r = getReg(Op0, MBB, IP);
889
890 // Before we do a comparison, we have to make sure that we're truncating our
891 // registers appropriately.
892 if (Class == cByte) {
893 unsigned TmpReg = makeAnotherReg(CompTy);
894 if (CompTy->isSigned())
895 BuildMI(*MBB, IP, PPC::EXTSB, 1, TmpReg).addReg(Op0r);
896 else
897 BuildMI(*MBB, IP, PPC::RLWINM, 4, TmpReg).addReg(Op0r).addImm(0)
898 .addImm(24).addImm(31);
899 Op0r = TmpReg;
900 } else if (Class == cShort) {
901 unsigned TmpReg = makeAnotherReg(CompTy);
902 if (CompTy->isSigned())
903 BuildMI(*MBB, IP, PPC::EXTSH, 1, TmpReg).addReg(Op0r);
904 else
905 BuildMI(*MBB, IP, PPC::RLWINM, 4, TmpReg).addReg(Op0r).addImm(0)
906 .addImm(16).addImm(31);
907 Op0r = TmpReg;
908 }
909
910 // Use crand for lt, gt and crandc for le, ge
911 unsigned CROpcode = (OpNum == 2 || OpNum == 4) ? PPC::CRAND : PPC::CRANDC;
912 unsigned Opcode = CompTy->isSigned() ? PPC::CMPW : PPC::CMPLW;
913 unsigned OpcodeImm = CompTy->isSigned() ? PPC::CMPWI : PPC::CMPLWI;
914 if (Class == cLong) {
915 Opcode = CompTy->isSigned() ? PPC::CMPD : PPC::CMPLD;
916 OpcodeImm = CompTy->isSigned() ? PPC::CMPDI : PPC::CMPLDI;
917 }
918
919 // Special case handling of: cmp R, i
920 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
921 unsigned Op1v = CI->getRawValue() & 0xFFFF;
922
923 // Treat compare like ADDI for the purposes of immediate suitability
924 if (canUseAsImmediateForOpcode(CI, 0)) {
925 BuildMI(*MBB, IP, OpcodeImm, 2, PPC::CR0).addReg(Op0r).addSImm(Op1v);
926 } else {
927 unsigned Op1r = getReg(Op1, MBB, IP);
928 BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r);
929 }
930 return OpNum;
931 }
932
933 unsigned Op1r = getReg(Op1, MBB, IP);
934
935 switch (Class) {
936 default: assert(0 && "Unknown type class!");
937 case cByte:
938 case cShort:
939 case cInt:
940 case cLong:
941 BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r);
942 break;
943
944 case cFP32:
945 case cFP64:
946 emitUCOM(MBB, IP, Op0r, Op1r);
947 break;
948 }
949
950 return OpNum;
951}
952
953/// visitSetCondInst - emit code to calculate the condition via
954/// EmitComparison(), and possibly store a 0 or 1 to a register as a result
955///
956void ISel::visitSetCondInst(SetCondInst &I) {
957 if (canFoldSetCCIntoBranchOrSelect(&I))
958 return;
959
960 unsigned DestReg = getReg(I);
961 unsigned OpNum = I.getOpcode();
962 const Type *Ty = I.getOperand (0)->getType();
963
964 EmitComparison(OpNum, I.getOperand(0), I.getOperand(1), BB, BB->end());
965
966 unsigned Opcode = getPPCOpcodeForSetCCNumber(OpNum);
967 MachineBasicBlock *thisMBB = BB;
968 const BasicBlock *LLVM_BB = BB->getBasicBlock();
969 ilist<MachineBasicBlock>::iterator It = BB;
970 ++It;
971
972 // thisMBB:
973 // ...
974 // cmpTY cr0, r1, r2
975 // bCC copy1MBB
976 // b copy0MBB
977
978 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
979 // if we could insert other, non-terminator instructions after the
980 // bCC. But MBB->getFirstTerminator() can't understand this.
981 MachineBasicBlock *copy1MBB = new MachineBasicBlock(LLVM_BB);
982 F->getBasicBlockList().insert(It, copy1MBB);
983 BuildMI(BB, Opcode, 2).addReg(PPC::CR0).addMBB(copy1MBB);
984 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
985 F->getBasicBlockList().insert(It, copy0MBB);
986 BuildMI(BB, PPC::B, 1).addMBB(copy0MBB);
987 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
988 F->getBasicBlockList().insert(It, sinkMBB);
989 // Update machine-CFG edges
990 BB->addSuccessor(copy1MBB);
991 BB->addSuccessor(copy0MBB);
992
993 // copy1MBB:
994 // %TrueValue = li 1
995 // b sinkMBB
996 BB = copy1MBB;
997 unsigned TrueValue = makeAnotherReg(I.getType());
998 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
999 BuildMI(BB, PPC::B, 1).addMBB(sinkMBB);
1000 // Update machine-CFG edges
1001 BB->addSuccessor(sinkMBB);
1002
1003 // copy0MBB:
1004 // %FalseValue = li 0
1005 // fallthrough
1006 BB = copy0MBB;
1007 unsigned FalseValue = makeAnotherReg(I.getType());
1008 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1009 // Update machine-CFG edges
1010 BB->addSuccessor(sinkMBB);
1011
1012 // sinkMBB:
1013 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1014 // ...
1015 BB = sinkMBB;
1016 BuildMI(BB, PPC::PHI, 4, DestReg).addReg(FalseValue)
1017 .addMBB(copy0MBB).addReg(TrueValue).addMBB(copy1MBB);
1018}
1019
1020void ISel::visitSelectInst(SelectInst &SI) {
1021 unsigned DestReg = getReg(SI);
1022 MachineBasicBlock::iterator MII = BB->end();
1023 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1024 SI.getFalseValue(), DestReg);
1025}
1026
1027/// emitSelect - Common code shared between visitSelectInst and the constant
1028/// expression support.
1029/// FIXME: this is most likely broken in one or more ways. Namely, PowerPC has
1030/// no select instruction. FSEL only works for comparisons against zero.
1031void ISel::emitSelectOperation(MachineBasicBlock *MBB,
1032 MachineBasicBlock::iterator IP,
1033 Value *Cond, Value *TrueVal, Value *FalseVal,
1034 unsigned DestReg) {
1035 unsigned SelectClass = getClassB(TrueVal->getType());
1036 unsigned Opcode;
1037
1038 // See if we can fold the setcc into the select instruction, or if we have
1039 // to get the register of the Cond value
1040 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1041 // We successfully folded the setcc into the select instruction.
1042 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1043 OpNum = EmitComparison(OpNum, SCI->getOperand(0),SCI->getOperand(1),MBB,IP);
1044 Opcode = getPPCOpcodeForSetCCNumber(SCI->getOpcode());
1045 } else {
1046 unsigned CondReg = getReg(Cond, MBB, IP);
1047 BuildMI(*MBB, IP, PPC::CMPI, 2, PPC::CR0).addReg(CondReg).addSImm(0);
1048 Opcode = getPPCOpcodeForSetCCNumber(Instruction::SetNE);
1049 }
1050
1051 // thisMBB:
1052 // ...
1053 // cmpTY cr0, r1, r2
1054 // bCC copy1MBB
1055 // b copy0MBB
1056
1057 MachineBasicBlock *thisMBB = BB;
1058 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1059 ilist<MachineBasicBlock>::iterator It = BB;
1060 ++It;
1061
1062 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1063 // if we could insert other, non-terminator instructions after the
1064 // bCC. But MBB->getFirstTerminator() can't understand this.
1065 MachineBasicBlock *copy1MBB = new MachineBasicBlock(LLVM_BB);
1066 F->getBasicBlockList().insert(It, copy1MBB);
1067 BuildMI(BB, Opcode, 2).addReg(PPC::CR0).addMBB(copy1MBB);
1068 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1069 F->getBasicBlockList().insert(It, copy0MBB);
1070 BuildMI(BB, PPC::B, 1).addMBB(copy0MBB);
1071 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1072 F->getBasicBlockList().insert(It, sinkMBB);
1073 // Update machine-CFG edges
1074 BB->addSuccessor(copy1MBB);
1075 BB->addSuccessor(copy0MBB);
1076
1077 // copy1MBB:
1078 // %TrueValue = ...
1079 // b sinkMBB
1080 BB = copy1MBB;
1081 unsigned TrueValue = getReg(TrueVal, BB, BB->begin());
1082 BuildMI(BB, PPC::B, 1).addMBB(sinkMBB);
1083 // Update machine-CFG edges
1084 BB->addSuccessor(sinkMBB);
1085
1086 // copy0MBB:
1087 // %FalseValue = ...
1088 // fallthrough
1089 BB = copy0MBB;
1090 unsigned FalseValue = getReg(FalseVal, BB, BB->begin());
1091 // Update machine-CFG edges
1092 BB->addSuccessor(sinkMBB);
1093
1094 // sinkMBB:
1095 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1096 // ...
1097 BB = sinkMBB;
1098 BuildMI(BB, PPC::PHI, 4, DestReg).addReg(FalseValue)
1099 .addMBB(copy0MBB).addReg(TrueValue).addMBB(copy1MBB);
1100 return;
1101}
1102
1103
1104
1105/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1106/// operand, in the specified target register.
1107///
1108void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
1109 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
1110
1111 Value *Val = VR.Val;
1112 const Type *Ty = VR.Ty;
1113 if (Val) {
1114 if (Constant *C = dyn_cast<Constant>(Val)) {
1115 Val = ConstantExpr::getCast(C, Type::IntTy);
1116 if (isa<ConstantExpr>(Val)) // Could not fold
1117 Val = C;
1118 else
1119 Ty = Type::IntTy; // Folded!
1120 }
1121
1122 // If this is a simple constant, just emit a load directly to avoid the copy
1123 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1124 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
1125
1126 if (TheVal < 32768 && TheVal >= -32768) {
1127 BuildMI(BB, PPC::LI, 1, targetReg).addSImm(TheVal);
1128 } else {
1129 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1130 BuildMI(BB, PPC::LIS, 1, TmpReg).addSImm(TheVal >> 16);
1131 BuildMI(BB, PPC::ORI, 2, targetReg).addReg(TmpReg)
1132 .addImm(TheVal & 0xFFFF);
1133 }
1134 return;
1135 }
1136 }
1137
1138 // Make sure we have the register number for this value...
1139 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1140 switch (getClassB(Ty)) {
1141 case cByte:
1142 // Extend value into target register (8->32)
1143 if (isUnsigned)
1144 BuildMI(BB, PPC::RLWINM, 4, targetReg).addReg(Reg).addZImm(0)
1145 .addZImm(24).addZImm(31);
1146 else
1147 BuildMI(BB, PPC::EXTSB, 1, targetReg).addReg(Reg);
1148 break;
1149 case cShort:
1150 // Extend value into target register (16->32)
1151 if (isUnsigned)
1152 BuildMI(BB, PPC::RLWINM, 4, targetReg).addReg(Reg).addZImm(0)
1153 .addZImm(16).addZImm(31);
1154 else
1155 BuildMI(BB, PPC::EXTSH, 1, targetReg).addReg(Reg);
1156 break;
1157 case cInt:
1158 case cLong:
1159 // Move value into target register (32->32)
1160 BuildMI(BB, PPC::OR, 2, targetReg).addReg(Reg).addReg(Reg);
1161 break;
1162 default:
1163 assert(0 && "Unpromotable operand class in promote32");
1164 }
1165}
1166
1167/// visitReturnInst - implemented with BLR
1168///
1169void ISel::visitReturnInst(ReturnInst &I) {
1170 // Only do the processing if this is a non-void return
1171 if (I.getNumOperands() > 0) {
1172 Value *RetVal = I.getOperand(0);
1173 switch (getClassB(RetVal->getType())) {
1174 case cByte: // integral return values: extend or move into r3 and return
1175 case cShort:
1176 case cInt:
1177 case cLong:
1178 promote32(PPC::R3, ValueRecord(RetVal));
1179 break;
1180 case cFP32:
1181 case cFP64: { // Floats & Doubles: Return in f1
1182 unsigned RetReg = getReg(RetVal);
1183 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(RetReg);
1184 break;
1185 }
1186 default:
1187 visitInstruction(I);
1188 }
1189 }
Misha Brukmana1b6ae92004-08-12 03:30:03 +00001190 BuildMI(BB, PPC::BLR, 1).addImm(1);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001191}
1192
1193// getBlockAfter - Return the basic block which occurs lexically after the
1194// specified one.
1195static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1196 Function::iterator I = BB; ++I; // Get iterator to next block
1197 return I != BB->getParent()->end() ? &*I : 0;
1198}
1199
1200/// visitBranchInst - Handle conditional and unconditional branches here. Note
1201/// that since code layout is frozen at this point, that if we are trying to
1202/// jump to a block that is the immediate successor of the current block, we can
1203/// just make a fall-through (but we don't currently).
1204///
1205void ISel::visitBranchInst(BranchInst &BI) {
1206 // Update machine-CFG edges
1207 BB->addSuccessor(MBBMap[BI.getSuccessor(0)]);
1208 if (BI.isConditional())
1209 BB->addSuccessor(MBBMap[BI.getSuccessor(1)]);
1210
1211 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1212
1213 if (!BI.isConditional()) { // Unconditional branch?
1214 if (BI.getSuccessor(0) != NextBB)
1215 BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
1216 return;
1217 }
1218
1219 // See if we can fold the setcc into the branch itself...
1220 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
1221 if (SCI == 0) {
1222 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1223 // computed some other way...
1224 unsigned condReg = getReg(BI.getCondition());
1225 BuildMI(BB, PPC::CMPLI, 3, PPC::CR0).addImm(0).addReg(condReg)
1226 .addImm(0);
1227 if (BI.getSuccessor(1) == NextBB) {
1228 if (BI.getSuccessor(0) != NextBB)
1229 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(PPC::BNE)
1230 .addMBB(MBBMap[BI.getSuccessor(0)])
1231 .addMBB(MBBMap[BI.getSuccessor(1)]);
1232 } else {
1233 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(PPC::BEQ)
1234 .addMBB(MBBMap[BI.getSuccessor(1)])
1235 .addMBB(MBBMap[BI.getSuccessor(0)]);
1236 if (BI.getSuccessor(0) != NextBB)
1237 BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
1238 }
1239 return;
1240 }
1241
1242 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1243 unsigned Opcode = getPPCOpcodeForSetCCNumber(SCI->getOpcode());
1244 MachineBasicBlock::iterator MII = BB->end();
1245 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
1246
1247 if (BI.getSuccessor(0) != NextBB) {
1248 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(Opcode)
1249 .addMBB(MBBMap[BI.getSuccessor(0)])
1250 .addMBB(MBBMap[BI.getSuccessor(1)]);
1251 if (BI.getSuccessor(1) != NextBB)
1252 BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
1253 } else {
1254 // Change to the inverse condition...
1255 if (BI.getSuccessor(1) != NextBB) {
Misha Brukmanadde6992004-08-17 04:57:37 +00001256 Opcode = PPC64InstrInfo::invertPPCBranchOpcode(Opcode);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001257 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(Opcode)
1258 .addMBB(MBBMap[BI.getSuccessor(1)])
1259 .addMBB(MBBMap[BI.getSuccessor(0)]);
1260 }
1261 }
1262}
1263
1264/// doCall - This emits an abstract call instruction, setting up the arguments
1265/// and the return value as appropriate. For the actual function call itself,
1266/// it inserts the specified CallMI instruction into the stream.
1267///
1268void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1269 const std::vector<ValueRecord> &Args, bool isVarArg) {
1270 // Count how many bytes are to be pushed on the stack, including the linkage
1271 // area, and parameter passing area.
1272 unsigned NumBytes = 24;
1273 unsigned ArgOffset = 24;
1274
1275 if (!Args.empty()) {
1276 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1277 switch (getClassB(Args[i].Ty)) {
1278 case cByte: case cShort: case cInt:
1279 NumBytes += 4; break;
1280 case cLong:
1281 NumBytes += 8; break;
1282 case cFP32:
1283 NumBytes += 4; break;
1284 case cFP64:
1285 NumBytes += 8; break;
1286 break;
1287 default: assert(0 && "Unknown class!");
1288 }
1289
1290 // Just to be safe, we'll always reserve the full 32 bytes worth of
1291 // argument passing space in case any called code gets funky on us.
1292 if (NumBytes < 24 + 32) NumBytes = 24 + 32;
1293
1294 // Adjust the stack pointer for the new arguments...
1295 // These functions are automatically eliminated by the prolog/epilog pass
1296 BuildMI(BB, PPC::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
1297
1298 // Arguments go on the stack in reverse order, as specified by the ABI.
1299 // Offset to the paramater area on the stack is 24.
1300 int GPR_remaining = 8, FPR_remaining = 13;
1301 unsigned GPR_idx = 0, FPR_idx = 0;
1302 static const unsigned GPR[] = {
1303 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1304 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1305 };
1306 static const unsigned FPR[] = {
1307 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
1308 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
1309 PPC::F13
1310 };
1311
1312 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1313 unsigned ArgReg;
1314 switch (getClassB(Args[i].Ty)) {
1315 case cByte:
1316 case cShort:
1317 // Promote arg to 32 bits wide into a temporary register...
1318 ArgReg = makeAnotherReg(Type::UIntTy);
1319 promote32(ArgReg, Args[i]);
1320
1321 // Reg or stack?
1322 if (GPR_remaining > 0) {
1323 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg)
1324 .addReg(ArgReg);
1325 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1326 }
1327 if (GPR_remaining <= 0 || isVarArg) {
1328 BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset)
1329 .addReg(PPC::R1);
1330 }
1331 break;
1332 case cInt:
1333 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1334
1335 // Reg or stack?
1336 if (GPR_remaining > 0) {
1337 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg)
1338 .addReg(ArgReg);
1339 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1340 }
1341 if (GPR_remaining <= 0 || isVarArg) {
1342 BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset)
1343 .addReg(PPC::R1);
1344 }
1345 break;
1346 case cLong:
1347 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1348
1349 // Reg or stack?
1350 if (GPR_remaining > 0) {
1351 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg)
1352 .addReg(ArgReg);
1353 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1354 }
1355 if (GPR_remaining <= 0 || isVarArg) {
1356 BuildMI(BB, PPC::STD, 3).addReg(ArgReg).addSImm(ArgOffset)
1357 .addReg(PPC::R1);
1358 }
1359 ArgOffset += 4; // 8 byte entry, not 4.
1360 break;
1361 case cFP32:
1362 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1363 // Reg or stack?
1364 if (FPR_remaining > 0) {
1365 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgReg);
1366 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1367 FPR_remaining--;
1368 FPR_idx++;
1369
1370 // If this is a vararg function, and there are GPRs left, also
1371 // pass the float in an int. Otherwise, put it on the stack.
1372 if (isVarArg) {
1373 BuildMI(BB, PPC::STFS, 3).addReg(ArgReg).addSImm(ArgOffset)
Misha Brukman1c514ec2004-08-19 16:29:25 +00001374 .addReg(PPC::R1);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001375 if (GPR_remaining > 0) {
1376 BuildMI(BB, PPC::LWZ, 2, GPR[GPR_idx])
1377 .addSImm(ArgOffset).addReg(ArgReg);
1378 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1379 }
1380 }
1381 } else {
1382 BuildMI(BB, PPC::STFS, 3).addReg(ArgReg).addSImm(ArgOffset)
1383 .addReg(PPC::R1);
1384 }
1385 break;
1386 case cFP64:
1387 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1388 // Reg or stack?
1389 if (FPR_remaining > 0) {
1390 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgReg);
1391 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1392 FPR_remaining--;
1393 FPR_idx++;
1394 // For vararg functions, must pass doubles via int regs as well
1395 if (isVarArg) {
1396 BuildMI(BB, PPC::STFD, 3).addReg(ArgReg).addSImm(ArgOffset)
1397 .addReg(PPC::R1);
1398
1399 if (GPR_remaining > 0) {
1400 BuildMI(BB, PPC::LD, 2, GPR[GPR_idx]).addSImm(ArgOffset)
1401 .addReg(PPC::R1);
1402 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1403 }
1404 }
1405 } else {
1406 BuildMI(BB, PPC::STFD, 3).addReg(ArgReg).addSImm(ArgOffset)
1407 .addReg(PPC::R1);
1408 }
1409 // Doubles use 8 bytes
1410 ArgOffset += 4;
1411 break;
1412
1413 default: assert(0 && "Unknown class!");
1414 }
1415 ArgOffset += 4;
1416 GPR_remaining--;
1417 GPR_idx++;
1418 }
1419 } else {
1420 BuildMI(BB, PPC::ADJCALLSTACKDOWN, 1).addImm(0);
1421 }
1422
1423 BuildMI(BB, PPC::IMPLICIT_DEF, 0, PPC::LR);
1424 BB->push_back(CallMI);
Misha Brukmana1b6ae92004-08-12 03:30:03 +00001425 BuildMI(BB, PPC::NOP, 0);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001426
1427 // These functions are automatically eliminated by the prolog/epilog pass
1428 BuildMI(BB, PPC::ADJCALLSTACKUP, 1).addImm(NumBytes);
1429
1430 // If there is a return value, scavenge the result from the location the call
1431 // leaves it in...
1432 //
1433 if (Ret.Ty != Type::VoidTy) {
1434 unsigned DestClass = getClassB(Ret.Ty);
1435 switch (DestClass) {
1436 case cByte:
1437 case cShort:
1438 case cInt:
1439 case cLong:
1440 // Integral results are in r3
1441 BuildMI(BB, PPC::OR, 2, Ret.Reg).addReg(PPC::R3).addReg(PPC::R3);
1442 break;
1443 case cFP32: // Floating-point return values live in f1
1444 case cFP64:
1445 BuildMI(BB, PPC::FMR, 1, Ret.Reg).addReg(PPC::F1);
1446 break;
1447 default: assert(0 && "Unknown class!");
1448 }
1449 }
1450}
1451
1452
1453/// visitCallInst - Push args on stack and do a procedure call instruction.
1454void ISel::visitCallInst(CallInst &CI) {
1455 MachineInstr *TheCall;
1456 Function *F = CI.getCalledFunction();
1457 if (F) {
1458 // Is it an intrinsic function call?
1459 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
1460 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1461 return;
1462 }
1463 // Emit a CALL instruction with PC-relative displacement.
1464 TheCall = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(F, true);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001465 } else { // Emit an indirect call through the CTR
1466 unsigned Reg = getReg(CI.getCalledValue());
1467 BuildMI(BB, PPC::MTCTR, 1).addReg(Reg);
1468 TheCall = BuildMI(PPC::CALLindirect, 2).addZImm(20).addZImm(0);
1469 }
1470
1471 std::vector<ValueRecord> Args;
1472 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
1473 Args.push_back(ValueRecord(CI.getOperand(i)));
1474
1475 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1476 bool isVarArg = F ? F->getFunctionType()->isVarArg() : true;
1477 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args, isVarArg);
1478}
1479
1480
1481/// dyncastIsNan - Return the operand of an isnan operation if this is an isnan.
1482///
1483static Value *dyncastIsNan(Value *V) {
1484 if (CallInst *CI = dyn_cast<CallInst>(V))
1485 if (Function *F = CI->getCalledFunction())
1486 if (F->getIntrinsicID() == Intrinsic::isunordered)
1487 return CI->getOperand(1);
1488 return 0;
1489}
1490
1491/// isOnlyUsedByUnorderedComparisons - Return true if this value is only used by
1492/// or's whos operands are all calls to the isnan predicate.
1493static bool isOnlyUsedByUnorderedComparisons(Value *V) {
1494 assert(dyncastIsNan(V) && "The value isn't an isnan call!");
1495
1496 // Check all uses, which will be or's of isnans if this predicate is true.
1497 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
1498 Instruction *I = cast<Instruction>(*UI);
1499 if (I->getOpcode() != Instruction::Or) return false;
1500 if (I->getOperand(0) != V && !dyncastIsNan(I->getOperand(0))) return false;
1501 if (I->getOperand(1) != V && !dyncastIsNan(I->getOperand(1))) return false;
1502 }
1503
1504 return true;
1505}
1506
1507/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1508/// function, lowering any calls to unknown intrinsic functions into the
1509/// equivalent LLVM code.
1510///
1511void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1512 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1513 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1514 if (CallInst *CI = dyn_cast<CallInst>(I++))
1515 if (Function *F = CI->getCalledFunction())
1516 switch (F->getIntrinsicID()) {
1517 case Intrinsic::not_intrinsic:
1518 case Intrinsic::vastart:
1519 case Intrinsic::vacopy:
1520 case Intrinsic::vaend:
1521 case Intrinsic::returnaddress:
1522 case Intrinsic::frameaddress:
1523 // FIXME: should lower these ourselves
1524 // case Intrinsic::isunordered:
1525 // case Intrinsic::memcpy: -> doCall(). system memcpy almost
1526 // guaranteed to be faster than anything we generate ourselves
1527 // We directly implement these intrinsics
1528 break;
1529 case Intrinsic::readio: {
1530 // On PPC, memory operations are in-order. Lower this intrinsic
1531 // into a volatile load.
1532 Instruction *Before = CI->getPrev();
1533 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1534 CI->replaceAllUsesWith(LI);
1535 BB->getInstList().erase(CI);
1536 break;
1537 }
1538 case Intrinsic::writeio: {
1539 // On PPC, memory operations are in-order. Lower this intrinsic
1540 // into a volatile store.
1541 Instruction *Before = CI->getPrev();
1542 StoreInst *SI = new StoreInst(CI->getOperand(1),
1543 CI->getOperand(2), true, CI);
1544 CI->replaceAllUsesWith(SI);
1545 BB->getInstList().erase(CI);
1546 break;
1547 }
1548 default:
1549 // All other intrinsic calls we must lower.
1550 Instruction *Before = CI->getPrev();
1551 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1552 if (Before) { // Move iterator to instruction after call
1553 I = Before; ++I;
1554 } else {
1555 I = BB->begin();
1556 }
1557 }
1558}
1559
1560void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1561 unsigned TmpReg1, TmpReg2, TmpReg3;
1562 switch (ID) {
1563 case Intrinsic::vastart:
1564 // Get the address of the first vararg value...
1565 TmpReg1 = getReg(CI);
1566 addFrameReference(BuildMI(BB, PPC::ADDI, 2, TmpReg1), VarArgsFrameIndex,
1567 0, false);
1568 return;
1569
1570 case Intrinsic::vacopy:
1571 TmpReg1 = getReg(CI);
1572 TmpReg2 = getReg(CI.getOperand(1));
1573 BuildMI(BB, PPC::OR, 2, TmpReg1).addReg(TmpReg2).addReg(TmpReg2);
1574 return;
1575 case Intrinsic::vaend: return;
1576
1577 case Intrinsic::returnaddress:
1578 TmpReg1 = getReg(CI);
1579 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1580 MachineFrameInfo *MFI = F->getFrameInfo();
1581 unsigned NumBytes = MFI->getStackSize();
1582
1583 BuildMI(BB, PPC::LWZ, 2, TmpReg1).addSImm(NumBytes+8)
1584 .addReg(PPC::R1);
1585 } else {
1586 // Values other than zero are not implemented yet.
1587 BuildMI(BB, PPC::LI, 1, TmpReg1).addSImm(0);
1588 }
1589 return;
1590
1591 case Intrinsic::frameaddress:
1592 TmpReg1 = getReg(CI);
1593 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1594 BuildMI(BB, PPC::OR, 2, TmpReg1).addReg(PPC::R1).addReg(PPC::R1);
1595 } else {
1596 // Values other than zero are not implemented yet.
1597 BuildMI(BB, PPC::LI, 1, TmpReg1).addSImm(0);
1598 }
1599 return;
1600
1601#if 0
1602 // This may be useful for supporting isunordered
1603 case Intrinsic::isnan:
1604 // If this is only used by 'isunordered' style comparisons, don't emit it.
1605 if (isOnlyUsedByUnorderedComparisons(&CI)) return;
1606 TmpReg1 = getReg(CI.getOperand(1));
1607 emitUCOM(BB, BB->end(), TmpReg1, TmpReg1);
1608 TmpReg2 = makeAnotherReg(Type::IntTy);
1609 BuildMI(BB, PPC::MFCR, TmpReg2);
1610 TmpReg3 = getReg(CI);
1611 BuildMI(BB, PPC::RLWINM, 4, TmpReg3).addReg(TmpReg2).addImm(4).addImm(31).addImm(31);
1612 return;
1613#endif
1614
1615 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
1616 }
1617}
1618
1619/// visitSimpleBinary - Implement simple binary operators for integral types...
1620/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1621/// Xor.
1622///
1623void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1624 unsigned DestReg = getReg(B);
1625 MachineBasicBlock::iterator MI = BB->end();
1626 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1627 unsigned Class = getClassB(B.getType());
1628
1629 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
1630}
1631
1632/// emitBinaryFPOperation - This method handles emission of floating point
1633/// Add (0), Sub (1), Mul (2), and Div (3) operations.
1634void ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
1635 MachineBasicBlock::iterator IP,
1636 Value *Op0, Value *Op1,
1637 unsigned OperatorClass, unsigned DestReg) {
1638
Nate Begeman81d265d2004-08-19 05:20:54 +00001639 static const unsigned OpcodeTab[][4] = {
1640 { PPC::FADDS, PPC::FSUBS, PPC::FMULS, PPC::FDIVS }, // Float
1641 { PPC::FADD, PPC::FSUB, PPC::FMUL, PPC::FDIV }, // Double
1642 };
Misha Brukmanca9309f2004-08-11 23:42:15 +00001643
Misha Brukmanca9309f2004-08-11 23:42:15 +00001644 // Special case: R1 = op <const fp>, R2
1645 if (ConstantFP *Op0C = dyn_cast<ConstantFP>(Op0))
1646 if (Op0C->isExactlyValue(-0.0) && OperatorClass == 1) {
1647 // -0.0 - X === -X
1648 unsigned op1Reg = getReg(Op1, BB, IP);
1649 BuildMI(*BB, IP, PPC::FNEG, 1, DestReg).addReg(op1Reg);
1650 return;
Misha Brukmanca9309f2004-08-11 23:42:15 +00001651 }
1652
Nate Begeman81d265d2004-08-19 05:20:54 +00001653 unsigned Opcode = OpcodeTab[Op0->getType() == Type::DoubleTy][OperatorClass];
Misha Brukmanca9309f2004-08-11 23:42:15 +00001654 unsigned Op0r = getReg(Op0, BB, IP);
1655 unsigned Op1r = getReg(Op1, BB, IP);
1656 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1657}
1658
1659/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1660/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1661/// Or, 4 for Xor.
1662///
1663/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1664/// and constant expression support.
1665///
1666void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
1667 MachineBasicBlock::iterator IP,
1668 Value *Op0, Value *Op1,
1669 unsigned OperatorClass, unsigned DestReg) {
1670 unsigned Class = getClassB(Op0->getType());
1671
1672 // Arithmetic and Bitwise operators
1673 static const unsigned OpcodeTab[] = {
1674 PPC::ADD, PPC::SUB, PPC::AND, PPC::OR, PPC::XOR
1675 };
1676 static const unsigned ImmOpcodeTab[] = {
1677 PPC::ADDI, PPC::SUBI, PPC::ANDIo, PPC::ORI, PPC::XORI
1678 };
1679 static const unsigned RImmOpcodeTab[] = {
1680 PPC::ADDI, PPC::SUBFIC, PPC::ANDIo, PPC::ORI, PPC::XORI
1681 };
1682
1683 if (Class == cFP32 || Class == cFP64) {
1684 assert(OperatorClass < 2 && "No logical ops for FP!");
1685 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
1686 return;
1687 }
1688
1689 if (Op0->getType() == Type::BoolTy) {
1690 if (OperatorClass == 3)
1691 // If this is an or of two isnan's, emit an FP comparison directly instead
1692 // of or'ing two isnan's together.
1693 if (Value *LHS = dyncastIsNan(Op0))
1694 if (Value *RHS = dyncastIsNan(Op1)) {
1695 unsigned Op0Reg = getReg(RHS, MBB, IP), Op1Reg = getReg(LHS, MBB, IP);
1696 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1697 emitUCOM(MBB, IP, Op0Reg, Op1Reg);
1698 BuildMI(*MBB, IP, PPC::MFCR, TmpReg);
1699 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(TmpReg).addImm(4)
1700 .addImm(31).addImm(31);
1701 return;
1702 }
1703 }
1704
1705 // Special case: op <const int>, Reg
1706 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) {
1707 // sub 0, X -> subfic
1708 if (OperatorClass == 1 && canUseAsImmediateForOpcode(CI, 0)) {
1709 unsigned Op1r = getReg(Op1, MBB, IP);
1710 int imm = CI->getRawValue() & 0xFFFF;
1711 BuildMI(*MBB, IP, PPC::SUBFIC, 2, DestReg).addReg(Op1r).addSImm(imm);
1712 return;
1713 }
1714
1715 // If it is easy to do, swap the operands and emit an immediate op
1716 if (Class != cLong && OperatorClass != 1 &&
1717 canUseAsImmediateForOpcode(CI, OperatorClass)) {
1718 unsigned Op1r = getReg(Op1, MBB, IP);
1719 int imm = CI->getRawValue() & 0xFFFF;
1720
1721 if (OperatorClass < 2)
1722 BuildMI(*MBB, IP, RImmOpcodeTab[OperatorClass], 2, DestReg).addReg(Op1r)
1723 .addSImm(imm);
1724 else
1725 BuildMI(*MBB, IP, RImmOpcodeTab[OperatorClass], 2, DestReg).addReg(Op1r)
1726 .addZImm(imm);
1727 return;
1728 }
1729 }
1730
1731 // Special case: op Reg, <const int>
1732 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1733 unsigned Op0r = getReg(Op0, MBB, IP);
1734
1735 // xor X, -1 -> not X
1736 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1737 BuildMI(*MBB, IP, PPC::NOR, 2, DestReg).addReg(Op0r).addReg(Op0r);
1738 return;
1739 }
1740
1741 if (canUseAsImmediateForOpcode(Op1C, OperatorClass)) {
1742 int immediate = Op1C->getRawValue() & 0xFFFF;
1743
1744 if (OperatorClass < 2)
1745 BuildMI(*MBB, IP, ImmOpcodeTab[OperatorClass], 2,DestReg).addReg(Op0r)
1746 .addSImm(immediate);
1747 else
1748 BuildMI(*MBB, IP, ImmOpcodeTab[OperatorClass], 2,DestReg).addReg(Op0r)
1749 .addZImm(immediate);
1750 } else {
1751 unsigned Op1r = getReg(Op1, MBB, IP);
1752 BuildMI(*MBB, IP, OpcodeTab[OperatorClass], 2, DestReg).addReg(Op0r)
1753 .addReg(Op1r);
1754 }
1755 return;
1756 }
1757
1758 // We couldn't generate an immediate variant of the op, load both halves into
1759 // registers and emit the appropriate opcode.
1760 unsigned Op0r = getReg(Op0, MBB, IP);
1761 unsigned Op1r = getReg(Op1, MBB, IP);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001762 unsigned Opcode = OpcodeTab[OperatorClass];
1763 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001764}
1765
1766// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1767// returns zero when the input is not exactly a power of two.
1768static unsigned ExactLog2(unsigned Val) {
1769 if (Val == 0 || (Val & (Val-1))) return 0;
1770 unsigned Count = 0;
1771 while (Val != 1) {
1772 Val >>= 1;
1773 ++Count;
1774 }
1775 return Count;
1776}
1777
1778/// doMultiply - Emit appropriate instructions to multiply together the
1779/// Values Op0 and Op1, and put the result in DestReg.
1780///
1781void ISel::doMultiply(MachineBasicBlock *MBB,
1782 MachineBasicBlock::iterator IP,
1783 unsigned DestReg, Value *Op0, Value *Op1) {
1784 unsigned Class0 = getClass(Op0->getType());
1785 unsigned Class1 = getClass(Op1->getType());
1786
1787 unsigned Op0r = getReg(Op0, MBB, IP);
1788 unsigned Op1r = getReg(Op1, MBB, IP);
1789
1790 // 64 x 64 -> 64
1791 if (Class0 == cLong && Class1 == cLong) {
Nate Begeman5a104b02004-08-13 02:20:47 +00001792 BuildMI(*MBB, IP, PPC::MULLD, 2, DestReg).addReg(Op0r).addReg(Op1r);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001793 return;
1794 }
1795
1796 // 64 x 32 or less, promote 32 to 64 and do a 64 x 64
1797 if (Class0 == cLong && Class1 <= cInt) {
Nate Begeman5a104b02004-08-13 02:20:47 +00001798 // FIXME: CLEAR or SIGN EXTEND Op1
1799 BuildMI(*MBB, IP, PPC::MULLD, 2, DestReg).addReg(Op0r).addReg(Op1r);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001800 return;
1801 }
1802
1803 // 32 x 32 -> 32
1804 if (Class0 <= cInt && Class1 <= cInt) {
1805 BuildMI(*MBB, IP, PPC::MULLW, 2, DestReg).addReg(Op0r).addReg(Op1r);
1806 return;
1807 }
1808
1809 assert(0 && "doMultiply cannot operate on unknown type!");
1810}
1811
1812/// doMultiplyConst - This method will multiply the value in Op0 by the
1813/// value of the ContantInt *CI
1814void ISel::doMultiplyConst(MachineBasicBlock *MBB,
1815 MachineBasicBlock::iterator IP,
1816 unsigned DestReg, Value *Op0, ConstantInt *CI) {
1817 unsigned Class = getClass(Op0->getType());
1818
1819 // Mul op0, 0 ==> 0
1820 if (CI->isNullValue()) {
1821 BuildMI(*MBB, IP, PPC::LI, 1, DestReg).addSImm(0);
1822 return;
1823 }
1824
1825 // Mul op0, 1 ==> op0
1826 if (CI->equalsInt(1)) {
1827 unsigned Op0r = getReg(Op0, MBB, IP);
1828 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(Op0r).addReg(Op0r);
1829 return;
1830 }
1831
1832 // If the element size is exactly a power of 2, use a shift to get it.
1833 if (unsigned Shift = ExactLog2(CI->getRawValue())) {
1834 ConstantUInt *ShiftCI = ConstantUInt::get(Type::UByteTy, Shift);
1835 emitShiftOperation(MBB, IP, Op0, ShiftCI, true, Op0->getType(), DestReg);
1836 return;
1837 }
1838
1839 // If 32 bits or less and immediate is in right range, emit mul by immediate
1840 if (Class == cByte || Class == cShort || Class == cInt) {
1841 if (canUseAsImmediateForOpcode(CI, 0)) {
1842 unsigned Op0r = getReg(Op0, MBB, IP);
1843 unsigned imm = CI->getRawValue() & 0xFFFF;
1844 BuildMI(*MBB, IP, PPC::MULLI, 2, DestReg).addReg(Op0r).addSImm(imm);
1845 return;
1846 }
1847 }
1848
1849 doMultiply(MBB, IP, DestReg, Op0, CI);
1850}
1851
1852void ISel::visitMul(BinaryOperator &I) {
1853 unsigned ResultReg = getReg(I);
1854
1855 Value *Op0 = I.getOperand(0);
1856 Value *Op1 = I.getOperand(1);
1857
1858 MachineBasicBlock::iterator IP = BB->end();
1859 emitMultiply(BB, IP, Op0, Op1, ResultReg);
1860}
1861
1862void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
1863 Value *Op0, Value *Op1, unsigned DestReg) {
1864 TypeClass Class = getClass(Op0->getType());
1865
1866 switch (Class) {
1867 case cByte:
1868 case cShort:
1869 case cInt:
1870 case cLong:
1871 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1872 doMultiplyConst(MBB, IP, DestReg, Op0, CI);
1873 } else {
1874 doMultiply(MBB, IP, DestReg, Op0, Op1);
1875 }
1876 return;
1877 case cFP32:
1878 case cFP64:
1879 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
1880 return;
1881 break;
1882 }
1883}
1884
1885
1886/// visitDivRem - Handle division and remainder instructions... these
1887/// instruction both require the same instructions to be generated, they just
1888/// select the result from a different register. Note that both of these
1889/// instructions work differently for signed and unsigned operands.
1890///
1891void ISel::visitDivRem(BinaryOperator &I) {
1892 unsigned ResultReg = getReg(I);
1893 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1894
1895 MachineBasicBlock::iterator IP = BB->end();
1896 emitDivRemOperation(BB, IP, Op0, Op1, I.getOpcode() == Instruction::Div,
1897 ResultReg);
1898}
1899
1900void ISel::emitDivRemOperation(MachineBasicBlock *BB,
1901 MachineBasicBlock::iterator IP,
1902 Value *Op0, Value *Op1, bool isDiv,
1903 unsigned ResultReg) {
1904 const Type *Ty = Op0->getType();
1905 unsigned Class = getClass(Ty);
1906 switch (Class) {
1907 case cFP32:
1908 if (isDiv) {
1909 // Floating point divide...
1910 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
1911 return;
1912 } else {
1913 // Floating point remainder via fmodf(float x, float y);
1914 unsigned Op0Reg = getReg(Op0, BB, IP);
1915 unsigned Op1Reg = getReg(Op1, BB, IP);
1916 MachineInstr *TheCall =
1917 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(fmodfFn, true);
1918 std::vector<ValueRecord> Args;
1919 Args.push_back(ValueRecord(Op0Reg, Type::FloatTy));
1920 Args.push_back(ValueRecord(Op1Reg, Type::FloatTy));
1921 doCall(ValueRecord(ResultReg, Type::FloatTy), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001922 }
1923 return;
1924 case cFP64:
1925 if (isDiv) {
1926 // Floating point divide...
1927 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
1928 return;
1929 } else {
1930 // Floating point remainder via fmod(double x, double y);
1931 unsigned Op0Reg = getReg(Op0, BB, IP);
1932 unsigned Op1Reg = getReg(Op1, BB, IP);
1933 MachineInstr *TheCall =
1934 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(fmodFn, true);
1935 std::vector<ValueRecord> Args;
1936 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
1937 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
1938 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001939 }
1940 return;
1941 case cLong: {
1942 static Function* const Funcs[] =
1943 { __moddi3Fn, __divdi3Fn, __umoddi3Fn, __udivdi3Fn };
1944 unsigned Op0Reg = getReg(Op0, BB, IP);
1945 unsigned Op1Reg = getReg(Op1, BB, IP);
1946 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
1947 MachineInstr *TheCall =
1948 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(Funcs[NameIdx], true);
1949
1950 std::vector<ValueRecord> Args;
1951 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
1952 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
1953 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00001954 return;
1955 }
1956 case cByte: case cShort: case cInt:
1957 break; // Small integrals, handled below...
1958 default: assert(0 && "Unknown class!");
1959 }
1960
1961 // Special case signed division by power of 2.
1962 if (isDiv)
1963 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1)) {
1964 assert(Class != cLong && "This doesn't handle 64-bit divides!");
1965 int V = CI->getValue();
1966
1967 if (V == 1) { // X /s 1 => X
1968 unsigned Op0Reg = getReg(Op0, BB, IP);
1969 BuildMI(*BB, IP, PPC::OR, 2, ResultReg).addReg(Op0Reg).addReg(Op0Reg);
1970 return;
1971 }
1972
1973 if (V == -1) { // X /s -1 => -X
1974 unsigned Op0Reg = getReg(Op0, BB, IP);
1975 BuildMI(*BB, IP, PPC::NEG, 1, ResultReg).addReg(Op0Reg);
1976 return;
1977 }
1978
1979 unsigned log2V = ExactLog2(V);
1980 if (log2V != 0 && Ty->isSigned()) {
1981 unsigned Op0Reg = getReg(Op0, BB, IP);
1982 unsigned TmpReg = makeAnotherReg(Op0->getType());
1983
1984 BuildMI(*BB, IP, PPC::SRAWI, 2, TmpReg).addReg(Op0Reg).addImm(log2V);
1985 BuildMI(*BB, IP, PPC::ADDZE, 1, ResultReg).addReg(TmpReg);
1986 return;
1987 }
1988 }
1989
1990 unsigned Op0Reg = getReg(Op0, BB, IP);
1991 unsigned Op1Reg = getReg(Op1, BB, IP);
1992 unsigned Opcode = Ty->isSigned() ? PPC::DIVW : PPC::DIVWU;
1993
1994 if (isDiv) {
1995 BuildMI(*BB, IP, Opcode, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1996 } else { // Remainder
1997 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
1998 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
1999
2000 BuildMI(*BB, IP, Opcode, 2, TmpReg1).addReg(Op0Reg).addReg(Op1Reg);
2001 BuildMI(*BB, IP, PPC::MULLW, 2, TmpReg2).addReg(TmpReg1).addReg(Op1Reg);
2002 BuildMI(*BB, IP, PPC::SUBF, 2, ResultReg).addReg(TmpReg2).addReg(Op0Reg);
2003 }
2004}
2005
2006
2007/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2008/// for constant immediate shift values, and for constant immediate
2009/// shift values equal to 1. Even the general case is sort of special,
2010/// because the shift amount has to be in CL, not just any old register.
2011///
2012void ISel::visitShiftInst(ShiftInst &I) {
2013 MachineBasicBlock::iterator IP = BB->end();
2014 emitShiftOperation(BB, IP, I.getOperand(0), I.getOperand(1),
2015 I.getOpcode() == Instruction::Shl, I.getType(),
2016 getReg(I));
2017}
2018
2019/// emitShiftOperation - Common code shared between visitShiftInst and
2020/// constant expression support.
2021///
2022void ISel::emitShiftOperation(MachineBasicBlock *MBB,
2023 MachineBasicBlock::iterator IP,
2024 Value *Op, Value *ShiftAmount, bool isLeftShift,
2025 const Type *ResultTy, unsigned DestReg) {
2026 unsigned SrcReg = getReg (Op, MBB, IP);
2027 bool isSigned = ResultTy->isSigned ();
2028 unsigned Class = getClass (ResultTy);
2029
2030 // Longs, as usual, are handled specially...
2031 if (Class == cLong) {
2032 // If we have a constant shift, we can generate much more efficient code
2033 // than otherwise...
2034 //
2035 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
2036 unsigned Amount = CUI->getValue();
Nate Begeman5a104b02004-08-13 02:20:47 +00002037 assert(Amount < 64 && "Invalid immediate shift amount!");
2038 if (isLeftShift) {
2039 BuildMI(*MBB, IP, PPC::RLDICR, 3, DestReg).addReg(SrcReg).addImm(Amount)
2040 .addImm(63-Amount);
2041 } else {
2042 if (isSigned) {
2043 BuildMI(*MBB, IP, PPC::SRADI, 2, DestReg).addReg(SrcReg)
2044 .addImm(Amount);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002045 } else {
Nate Begeman5a104b02004-08-13 02:20:47 +00002046 BuildMI(*MBB, IP, PPC::RLDICL, 3, DestReg).addReg(SrcReg)
2047 .addImm(64-Amount).addImm(Amount);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002048 }
2049 }
2050 } else {
Nate Begeman5a104b02004-08-13 02:20:47 +00002051 unsigned ShiftReg = getReg (ShiftAmount, MBB, IP);
2052
Misha Brukmanca9309f2004-08-11 23:42:15 +00002053 if (isLeftShift) {
Nate Begeman5a104b02004-08-13 02:20:47 +00002054 BuildMI(*MBB, IP, PPC::SLD, 2, DestReg).addReg(SrcReg).addReg(ShiftReg);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002055 } else {
Nate Begeman5a104b02004-08-13 02:20:47 +00002056 unsigned Opcode = (isSigned) ? PPC::SRAD : PPC::SRD;
2057 BuildMI(*MBB, IP, Opcode, DestReg).addReg(SrcReg).addReg(ShiftReg);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002058 }
2059 }
2060 return;
2061 }
2062
2063 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
2064 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2065 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
2066 unsigned Amount = CUI->getValue();
2067
2068 if (isLeftShift) {
2069 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2070 .addImm(Amount).addImm(0).addImm(31-Amount);
2071 } else {
2072 if (isSigned) {
2073 BuildMI(*MBB, IP, PPC::SRAWI,2,DestReg).addReg(SrcReg).addImm(Amount);
2074 } else {
2075 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2076 .addImm(32-Amount).addImm(Amount).addImm(31);
2077 }
2078 }
2079 } else { // The shift amount is non-constant.
Misha Brukman1c514ec2004-08-19 16:29:25 +00002080 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002081
2082 if (isLeftShift) {
2083 BuildMI(*MBB, IP, PPC::SLW, 2, DestReg).addReg(SrcReg)
2084 .addReg(ShiftAmountReg);
2085 } else {
2086 BuildMI(*MBB, IP, isSigned ? PPC::SRAW : PPC::SRW, 2, DestReg)
2087 .addReg(SrcReg).addReg(ShiftAmountReg);
2088 }
2089 }
2090}
2091
2092
2093/// visitLoadInst - Implement LLVM load instructions. Pretty straightforward
2094/// mapping of LLVM classes to PPC load instructions, with the exception of
2095/// signed byte loads, which need a sign extension following them.
2096///
2097void ISel::visitLoadInst(LoadInst &I) {
2098 // Immediate opcodes, for reg+imm addressing
2099 static const unsigned ImmOpcodes[] = {
2100 PPC::LBZ, PPC::LHZ, PPC::LWZ,
2101 PPC::LFS, PPC::LFD, PPC::LWZ
2102 };
2103 // Indexed opcodes, for reg+reg addressing
2104 static const unsigned IdxOpcodes[] = {
2105 PPC::LBZX, PPC::LHZX, PPC::LWZX,
2106 PPC::LFSX, PPC::LFDX, PPC::LWZX
2107 };
2108
2109 unsigned Class = getClassB(I.getType());
2110 unsigned ImmOpcode = ImmOpcodes[Class];
2111 unsigned IdxOpcode = IdxOpcodes[Class];
2112 unsigned DestReg = getReg(I);
2113 Value *SourceAddr = I.getOperand(0);
2114
2115 if (Class == cShort && I.getType()->isSigned()) ImmOpcode = PPC::LHA;
2116 if (Class == cShort && I.getType()->isSigned()) IdxOpcode = PPC::LHAX;
2117
2118 if (AllocaInst *AI = dyn_castFixedAlloca(SourceAddr)) {
2119 unsigned FI = getFixedSizedAllocaFI(AI);
2120 if (Class == cByte && I.getType()->isSigned()) {
2121 unsigned TmpReg = makeAnotherReg(I.getType());
2122 addFrameReference(BuildMI(BB, ImmOpcode, 2, TmpReg), FI);
2123 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2124 } else {
2125 addFrameReference(BuildMI(BB, ImmOpcode, 2, DestReg), FI);
2126 }
2127 return;
2128 }
2129
2130 // If this load is the only use of the GEP instruction that is its address,
2131 // then we can fold the GEP directly into the load instruction.
2132 // emitGEPOperation with a second to last arg of 'true' will place the
2133 // base register for the GEP into baseReg, and the constant offset from that
2134 // into offset. If the offset fits in 16 bits, we can emit a reg+imm store
2135 // otherwise, we copy the offset into another reg, and use reg+reg addressing.
2136 if (GetElementPtrInst *GEPI = canFoldGEPIntoLoadOrStore(SourceAddr)) {
2137 unsigned baseReg = getReg(GEPI);
2138 unsigned pendingAdd;
2139 ConstantSInt *offset;
2140
2141 emitGEPOperation(BB, BB->end(), GEPI->getOperand(0), GEPI->op_begin()+1,
2142 GEPI->op_end(), baseReg, true, &offset, &pendingAdd);
2143
2144 if (pendingAdd == 0 && Class != cLong &&
2145 canUseAsImmediateForOpcode(offset, 0)) {
2146 if (Class == cByte && I.getType()->isSigned()) {
2147 unsigned TmpReg = makeAnotherReg(I.getType());
2148 BuildMI(BB, ImmOpcode, 2, TmpReg).addSImm(offset->getValue())
2149 .addReg(baseReg);
2150 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2151 } else {
2152 BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(offset->getValue())
2153 .addReg(baseReg);
2154 }
2155 return;
2156 }
2157
2158 unsigned indexReg = (pendingAdd != 0) ? pendingAdd : getReg(offset);
2159
2160 if (Class == cByte && I.getType()->isSigned()) {
2161 unsigned TmpReg = makeAnotherReg(I.getType());
2162 BuildMI(BB, IdxOpcode, 2, TmpReg).addReg(indexReg).addReg(baseReg);
2163 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2164 } else {
2165 BuildMI(BB, IdxOpcode, 2, DestReg).addReg(indexReg).addReg(baseReg);
2166 }
2167 return;
2168 }
2169
2170 // The fallback case, where the load was from a source that could not be
2171 // folded into the load instruction.
2172 unsigned SrcAddrReg = getReg(SourceAddr);
2173
2174 if (Class == cByte && I.getType()->isSigned()) {
2175 unsigned TmpReg = makeAnotherReg(I.getType());
2176 BuildMI(BB, ImmOpcode, 2, TmpReg).addSImm(0).addReg(SrcAddrReg);
2177 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2178 } else {
2179 BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(0).addReg(SrcAddrReg);
2180 }
2181}
2182
2183/// visitStoreInst - Implement LLVM store instructions
2184///
2185void ISel::visitStoreInst(StoreInst &I) {
2186 // Immediate opcodes, for reg+imm addressing
2187 static const unsigned ImmOpcodes[] = {
2188 PPC::STB, PPC::STH, PPC::STW,
2189 PPC::STFS, PPC::STFD, PPC::STW
2190 };
2191 // Indexed opcodes, for reg+reg addressing
2192 static const unsigned IdxOpcodes[] = {
2193 PPC::STBX, PPC::STHX, PPC::STWX,
2194 PPC::STFSX, PPC::STFDX, PPC::STWX
2195 };
2196
2197 Value *SourceAddr = I.getOperand(1);
2198 const Type *ValTy = I.getOperand(0)->getType();
2199 unsigned Class = getClassB(ValTy);
2200 unsigned ImmOpcode = ImmOpcodes[Class];
2201 unsigned IdxOpcode = IdxOpcodes[Class];
2202 unsigned ValReg = getReg(I.getOperand(0));
2203
2204 // If this store is the only use of the GEP instruction that is its address,
2205 // then we can fold the GEP directly into the store instruction.
2206 // emitGEPOperation with a second to last arg of 'true' will place the
2207 // base register for the GEP into baseReg, and the constant offset from that
2208 // into offset. If the offset fits in 16 bits, we can emit a reg+imm store
2209 // otherwise, we copy the offset into another reg, and use reg+reg addressing.
2210 if (GetElementPtrInst *GEPI = canFoldGEPIntoLoadOrStore(SourceAddr)) {
2211 unsigned baseReg = getReg(GEPI);
2212 unsigned pendingAdd;
2213 ConstantSInt *offset;
2214
2215 emitGEPOperation(BB, BB->end(), GEPI->getOperand(0), GEPI->op_begin()+1,
2216 GEPI->op_end(), baseReg, true, &offset, &pendingAdd);
2217
2218 if (0 == pendingAdd && Class != cLong &&
2219 canUseAsImmediateForOpcode(offset, 0)) {
2220 BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(offset->getValue())
2221 .addReg(baseReg);
2222 return;
2223 }
2224
2225 unsigned indexReg = (pendingAdd != 0) ? pendingAdd : getReg(offset);
2226 BuildMI(BB, IdxOpcode, 3).addReg(ValReg).addReg(indexReg).addReg(baseReg);
2227 return;
2228 }
2229
2230 // If the store address wasn't the only use of a GEP, we fall back to the
2231 // standard path: store the ValReg at the value in AddressReg.
2232 unsigned AddressReg = getReg(I.getOperand(1));
2233 BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(0).addReg(AddressReg);
2234}
2235
2236
2237/// visitCastInst - Here we have various kinds of copying with or without sign
2238/// extension going on.
2239///
2240void ISel::visitCastInst(CastInst &CI) {
2241 Value *Op = CI.getOperand(0);
2242
2243 unsigned SrcClass = getClassB(Op->getType());
2244 unsigned DestClass = getClassB(CI.getType());
2245
2246 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2247 // of the case are GEP instructions, then the cast does not need to be
2248 // generated explicitly, it will be folded into the GEP.
2249 if (DestClass == cLong && SrcClass == cInt) {
2250 bool AllUsesAreGEPs = true;
2251 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2252 if (!isa<GetElementPtrInst>(*I)) {
2253 AllUsesAreGEPs = false;
2254 break;
2255 }
2256
2257 // No need to codegen this cast if all users are getelementptr instrs...
2258 if (AllUsesAreGEPs) return;
2259 }
2260
2261 unsigned DestReg = getReg(CI);
2262 MachineBasicBlock::iterator MI = BB->end();
2263 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
2264}
2265
2266/// emitCastOperation - Common code shared between visitCastInst and constant
2267/// expression cast support.
2268///
2269void ISel::emitCastOperation(MachineBasicBlock *MBB,
2270 MachineBasicBlock::iterator IP,
2271 Value *Src, const Type *DestTy,
2272 unsigned DestReg) {
2273 const Type *SrcTy = Src->getType();
2274 unsigned SrcClass = getClassB(SrcTy);
2275 unsigned DestClass = getClassB(DestTy);
2276 unsigned SrcReg = getReg(Src, MBB, IP);
2277
2278 // Implement casts to bool by using compare on the operand followed by set if
2279 // not zero on the result.
2280 if (DestTy == Type::BoolTy) {
2281 switch (SrcClass) {
2282 case cByte:
2283 case cShort:
2284 case cInt:
2285 case cLong: {
2286 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2287 BuildMI(*MBB, IP, PPC::ADDIC, 2, TmpReg).addReg(SrcReg).addSImm(-1);
2288 BuildMI(*MBB, IP, PPC::SUBFE, 2, DestReg).addReg(TmpReg).addReg(SrcReg);
2289 break;
2290 }
2291 case cFP32:
2292 case cFP64:
2293 // FSEL perhaps?
2294 std::cerr << "ERROR: Cast fp-to-bool not implemented!\n";
2295 abort();
2296 }
2297 return;
2298 }
2299
2300 // Handle cast of Float -> Double
2301 if (SrcClass == cFP32 && DestClass == cFP64) {
2302 BuildMI(*MBB, IP, PPC::FMR, 1, DestReg).addReg(SrcReg);
2303 return;
2304 }
2305
2306 // Handle cast of Double -> Float
2307 if (SrcClass == cFP64 && DestClass == cFP32) {
2308 BuildMI(*MBB, IP, PPC::FRSP, 1, DestReg).addReg(SrcReg);
2309 return;
2310 }
2311
2312 // Handle casts from integer to floating point now...
2313 if (DestClass == cFP32 || DestClass == cFP64) {
2314
2315 // Emit a library call for long to float conversion
2316 if (SrcClass == cLong) {
2317 std::vector<ValueRecord> Args;
2318 Args.push_back(ValueRecord(SrcReg, SrcTy));
2319 Function *floatFn = (DestClass == cFP32) ? __floatdisfFn : __floatdidfFn;
2320 MachineInstr *TheCall =
2321 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(floatFn, true);
2322 doCall(ValueRecord(DestReg, DestTy), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002323 return;
2324 }
2325
2326 // Make sure we're dealing with a full 32 bits
2327 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2328 promote32(TmpReg, ValueRecord(SrcReg, SrcTy));
2329
2330 SrcReg = TmpReg;
2331
2332 // Spill the integer to memory and reload it from there.
2333 // Also spill room for a special conversion constant
2334 int ConstantFrameIndex =
2335 F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData());
2336 int ValueFrameIdx =
2337 F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData());
2338
2339 unsigned constantHi = makeAnotherReg(Type::IntTy);
2340 unsigned constantLo = makeAnotherReg(Type::IntTy);
2341 unsigned ConstF = makeAnotherReg(Type::DoubleTy);
2342 unsigned TempF = makeAnotherReg(Type::DoubleTy);
2343
2344 if (!SrcTy->isSigned()) {
2345 BuildMI(*BB, IP, PPC::LIS, 1, constantHi).addSImm(0x4330);
2346 BuildMI(*BB, IP, PPC::LI, 1, constantLo).addSImm(0);
2347 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi),
2348 ConstantFrameIndex);
2349 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantLo),
2350 ConstantFrameIndex, 4);
2351 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi),
2352 ValueFrameIdx);
2353 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(SrcReg),
2354 ValueFrameIdx, 4);
2355 addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, ConstF),
2356 ConstantFrameIndex);
2357 addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, TempF), ValueFrameIdx);
2358 BuildMI(*BB, IP, PPC::FSUB, 2, DestReg).addReg(TempF).addReg(ConstF);
2359 } else {
2360 unsigned TempLo = makeAnotherReg(Type::IntTy);
2361 BuildMI(*BB, IP, PPC::LIS, 1, constantHi).addSImm(0x4330);
2362 BuildMI(*BB, IP, PPC::LIS, 1, constantLo).addSImm(0x8000);
2363 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi),
2364 ConstantFrameIndex);
2365 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantLo),
2366 ConstantFrameIndex, 4);
2367 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi),
2368 ValueFrameIdx);
2369 BuildMI(*BB, IP, PPC::XORIS, 2, TempLo).addReg(SrcReg).addImm(0x8000);
2370 addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(TempLo),
2371 ValueFrameIdx, 4);
2372 addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, ConstF),
2373 ConstantFrameIndex);
2374 addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, TempF), ValueFrameIdx);
2375 BuildMI(*BB, IP, PPC::FSUB, 2, DestReg).addReg(TempF).addReg(ConstF);
2376 }
2377 return;
2378 }
2379
2380 // Handle casts from floating point to integer now...
2381 if (SrcClass == cFP32 || SrcClass == cFP64) {
2382 static Function* const Funcs[] =
2383 { __fixsfdiFn, __fixdfdiFn, __fixunssfdiFn, __fixunsdfdiFn };
2384 // emit library call
2385 if (DestClass == cLong) {
2386 bool isDouble = SrcClass == cFP64;
2387 unsigned nameIndex = 2 * DestTy->isSigned() + isDouble;
2388 std::vector<ValueRecord> Args;
2389 Args.push_back(ValueRecord(SrcReg, SrcTy));
2390 Function *floatFn = Funcs[nameIndex];
2391 MachineInstr *TheCall =
2392 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(floatFn, true);
2393 doCall(ValueRecord(DestReg, DestTy), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002394 return;
2395 }
2396
2397 int ValueFrameIdx =
2398 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
2399
2400 if (DestTy->isSigned()) {
2401 unsigned TempReg = makeAnotherReg(Type::DoubleTy);
2402
2403 // Convert to integer in the FP reg and store it to a stack slot
2404 BuildMI(*BB, IP, PPC::FCTIWZ, 1, TempReg).addReg(SrcReg);
2405 addFrameReference(BuildMI(*BB, IP, PPC::STFD, 3)
2406 .addReg(TempReg), ValueFrameIdx);
2407
2408 // There is no load signed byte opcode, so we must emit a sign extend for
2409 // that particular size. Make sure to source the new integer from the
2410 // correct offset.
2411 if (DestClass == cByte) {
2412 unsigned TempReg2 = makeAnotherReg(DestTy);
2413 addFrameReference(BuildMI(*BB, IP, PPC::LBZ, 2, TempReg2),
2414 ValueFrameIdx, 7);
2415 BuildMI(*MBB, IP, PPC::EXTSB, DestReg).addReg(TempReg2);
2416 } else {
2417 int offset = (DestClass == cShort) ? 6 : 4;
2418 unsigned LoadOp = (DestClass == cShort) ? PPC::LHA : PPC::LWZ;
2419 addFrameReference(BuildMI(*BB, IP, LoadOp, 2, DestReg),
2420 ValueFrameIdx, offset);
2421 }
2422 } else {
2423 unsigned Zero = getReg(ConstantFP::get(Type::DoubleTy, 0.0f));
2424 double maxInt = (1LL << 32) - 1;
2425 unsigned MaxInt = getReg(ConstantFP::get(Type::DoubleTy, maxInt));
2426 double border = 1LL << 31;
2427 unsigned Border = getReg(ConstantFP::get(Type::DoubleTy, border));
2428 unsigned UseZero = makeAnotherReg(Type::DoubleTy);
2429 unsigned UseMaxInt = makeAnotherReg(Type::DoubleTy);
2430 unsigned UseChoice = makeAnotherReg(Type::DoubleTy);
2431 unsigned TmpReg = makeAnotherReg(Type::DoubleTy);
2432 unsigned TmpReg2 = makeAnotherReg(Type::DoubleTy);
2433 unsigned ConvReg = makeAnotherReg(Type::DoubleTy);
2434 unsigned IntTmp = makeAnotherReg(Type::IntTy);
2435 unsigned XorReg = makeAnotherReg(Type::IntTy);
2436 int FrameIdx =
2437 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
2438 // Update machine-CFG edges
2439 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2440 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2441 MachineBasicBlock *OldMBB = BB;
2442 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2443 F->getBasicBlockList().insert(It, XorMBB);
2444 F->getBasicBlockList().insert(It, PhiMBB);
2445 BB->addSuccessor(XorMBB);
2446 BB->addSuccessor(PhiMBB);
2447
2448 // Convert from floating point to unsigned 32-bit value
2449 // Use 0 if incoming value is < 0.0
2450 BuildMI(*BB, IP, PPC::FSEL, 3, UseZero).addReg(SrcReg).addReg(SrcReg)
2451 .addReg(Zero);
2452 // Use 2**32 - 1 if incoming value is >= 2**32
2453 BuildMI(*BB, IP, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(SrcReg);
2454 BuildMI(*BB, IP, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt)
2455 .addReg(UseZero).addReg(MaxInt);
2456 // Subtract 2**31
2457 BuildMI(*BB, IP, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2458 // Use difference if >= 2**31
2459 BuildMI(*BB, IP, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice)
2460 .addReg(Border);
2461 BuildMI(*BB, IP, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2462 .addReg(UseChoice);
2463 // Convert to integer
2464 BuildMI(*BB, IP, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2465 addFrameReference(BuildMI(*BB, IP, PPC::STFD, 3).addReg(ConvReg),
2466 FrameIdx);
2467 if (DestClass == cByte) {
2468 addFrameReference(BuildMI(*BB, IP, PPC::LBZ, 2, DestReg),
2469 FrameIdx, 7);
2470 } else if (DestClass == cShort) {
2471 addFrameReference(BuildMI(*BB, IP, PPC::LHZ, 2, DestReg),
2472 FrameIdx, 6);
2473 } if (DestClass == cInt) {
2474 addFrameReference(BuildMI(*BB, IP, PPC::LWZ, 2, IntTmp),
2475 FrameIdx, 4);
2476 BuildMI(*BB, IP, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2477 BuildMI(*BB, IP, PPC::B, 1).addMBB(XorMBB);
2478
2479 // XorMBB:
2480 // add 2**31 if input was >= 2**31
2481 BB = XorMBB;
2482 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2483 XorMBB->addSuccessor(PhiMBB);
2484
2485 // PhiMBB:
2486 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2487 BB = PhiMBB;
Misha Brukman1c514ec2004-08-19 16:29:25 +00002488 BuildMI(BB, PPC::PHI, 4, DestReg).addReg(IntTmp).addMBB(OldMBB)
Misha Brukmanca9309f2004-08-11 23:42:15 +00002489 .addReg(XorReg).addMBB(XorMBB);
2490 }
2491 }
2492 return;
2493 }
2494
2495 // Check our invariants
2496 assert((SrcClass <= cInt || SrcClass == cLong) &&
2497 "Unhandled source class for cast operation!");
2498 assert((DestClass <= cInt || DestClass == cLong) &&
2499 "Unhandled destination class for cast operation!");
2500
2501 bool sourceUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
2502 bool destUnsigned = DestTy->isUnsigned();
2503
2504 // Unsigned -> Unsigned, clear if larger
2505 if (sourceUnsigned && destUnsigned) {
2506 // handle long dest class now to keep switch clean
2507 if (DestClass == cLong) {
Nate Begeman5a104b02004-08-13 02:20:47 +00002508 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002509 return;
2510 }
2511
2512 // handle u{ byte, short, int } x u{ byte, short, int }
2513 unsigned clearBits = (SrcClass == cByte || DestClass == cByte) ? 24 : 16;
2514 switch (SrcClass) {
2515 case cByte:
2516 case cShort:
2517 if (SrcClass == DestClass)
2518 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2519 else
2520 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2521 .addImm(0).addImm(clearBits).addImm(31);
2522 break;
2523 case cLong:
2524 ++SrcReg;
2525 // Fall through
2526 case cInt:
2527 if (DestClass == cInt)
2528 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2529 else
2530 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2531 .addImm(0).addImm(clearBits).addImm(31);
2532 break;
2533 }
2534 return;
2535 }
2536
2537 // Signed -> Signed
2538 if (!sourceUnsigned && !destUnsigned) {
2539 // handle long dest class now to keep switch clean
2540 if (DestClass == cLong) {
Nate Begeman5a104b02004-08-13 02:20:47 +00002541 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002542 return;
2543 }
2544
2545 // handle { byte, short, int } x { byte, short, int }
2546 switch (SrcClass) {
2547 case cByte:
2548 if (DestClass == cByte)
2549 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2550 else
2551 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2552 break;
2553 case cShort:
2554 if (DestClass == cByte)
2555 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2556 else if (DestClass == cShort)
2557 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2558 else
2559 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2560 break;
Misha Brukmanca9309f2004-08-11 23:42:15 +00002561 case cInt:
Misha Brukmancc55ad52004-08-19 16:50:30 +00002562 case cLong:
Misha Brukmanca9309f2004-08-11 23:42:15 +00002563 if (DestClass == cByte)
2564 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2565 else if (DestClass == cShort)
2566 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2567 else
2568 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2569 break;
2570 }
2571 return;
2572 }
2573
2574 // Unsigned -> Signed
2575 if (sourceUnsigned && !destUnsigned) {
2576 // handle long dest class now to keep switch clean
2577 if (DestClass == cLong) {
Nate Begeman5a104b02004-08-13 02:20:47 +00002578 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002579 return;
2580 }
2581
2582 // handle u{ byte, short, int } -> { byte, short, int }
2583 switch (SrcClass) {
2584 case cByte:
2585 if (DestClass == cByte)
2586 // uByte 255 -> signed byte == -1
2587 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2588 else
2589 // uByte 255 -> signed short/int == 255
2590 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg).addImm(0)
2591 .addImm(24).addImm(31);
2592 break;
2593 case cShort:
2594 if (DestClass == cByte)
2595 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2596 else if (DestClass == cShort)
2597 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2598 else
2599 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg).addImm(0)
2600 .addImm(16).addImm(31);
2601 break;
2602 case cLong:
2603 ++SrcReg;
2604 // Fall through
2605 case cInt:
2606 if (DestClass == cByte)
2607 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2608 else if (DestClass == cShort)
2609 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2610 else
2611 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2612 break;
2613 }
2614 return;
2615 }
2616
2617 // Signed -> Unsigned
2618 if (!sourceUnsigned && destUnsigned) {
2619 // handle long dest class now to keep switch clean
2620 if (DestClass == cLong) {
Nate Begeman5a104b02004-08-13 02:20:47 +00002621 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002622 return;
2623 }
2624
2625 // handle { byte, short, int } -> u{ byte, short, int }
2626 unsigned clearBits = (DestClass == cByte) ? 24 : 16;
2627 switch (SrcClass) {
2628 case cByte:
2629 case cShort:
2630 if (DestClass == cByte || DestClass == cShort)
2631 // sbyte -1 -> ubyte 0x000000FF
2632 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2633 .addImm(0).addImm(clearBits).addImm(31);
2634 else
2635 // sbyte -1 -> ubyte 0xFFFFFFFF
2636 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2637 break;
Misha Brukmanca9309f2004-08-11 23:42:15 +00002638 case cInt:
Misha Brukman1c514ec2004-08-19 16:29:25 +00002639 case cLong:
Misha Brukmanca9309f2004-08-11 23:42:15 +00002640 if (DestClass == cInt)
2641 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2642 else
2643 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2644 .addImm(0).addImm(clearBits).addImm(31);
2645 break;
2646 }
2647 return;
2648 }
2649
2650 // Anything we haven't handled already, we can't (yet) handle at all.
2651 std::cerr << "Unhandled cast from " << SrcTy->getDescription()
2652 << "to " << DestTy->getDescription() << '\n';
2653 abort();
2654}
2655
2656/// visitVANextInst - Implement the va_next instruction...
2657///
2658void ISel::visitVANextInst(VANextInst &I) {
2659 unsigned VAList = getReg(I.getOperand(0));
2660 unsigned DestReg = getReg(I);
2661
2662 unsigned Size;
2663 switch (I.getArgType()->getTypeID()) {
2664 default:
2665 std::cerr << I;
2666 assert(0 && "Error: bad type for va_next instruction!");
2667 return;
2668 case Type::PointerTyID:
2669 case Type::UIntTyID:
2670 case Type::IntTyID:
2671 Size = 4;
2672 break;
2673 case Type::ULongTyID:
2674 case Type::LongTyID:
2675 case Type::DoubleTyID:
2676 Size = 8;
2677 break;
2678 }
2679
2680 // Increment the VAList pointer...
2681 BuildMI(BB, PPC::ADDI, 2, DestReg).addReg(VAList).addSImm(Size);
2682}
2683
2684void ISel::visitVAArgInst(VAArgInst &I) {
2685 unsigned VAList = getReg(I.getOperand(0));
2686 unsigned DestReg = getReg(I);
2687
2688 switch (I.getType()->getTypeID()) {
2689 default:
2690 std::cerr << I;
2691 assert(0 && "Error: bad type for va_next instruction!");
2692 return;
2693 case Type::PointerTyID:
2694 case Type::UIntTyID:
2695 case Type::IntTyID:
2696 BuildMI(BB, PPC::LWZ, 2, DestReg).addSImm(0).addReg(VAList);
2697 break;
2698 case Type::ULongTyID:
2699 case Type::LongTyID:
2700 BuildMI(BB, PPC::LD, 2, DestReg).addSImm(0).addReg(VAList);
2701 break;
2702 case Type::FloatTyID:
2703 BuildMI(BB, PPC::LFS, 2, DestReg).addSImm(0).addReg(VAList);
2704 break;
2705 case Type::DoubleTyID:
2706 BuildMI(BB, PPC::LFD, 2, DestReg).addSImm(0).addReg(VAList);
2707 break;
2708 }
2709}
2710
2711/// visitGetElementPtrInst - instruction-select GEP instructions
2712///
2713void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
2714 if (canFoldGEPIntoLoadOrStore(&I))
2715 return;
2716
2717 unsigned outputReg = getReg(I);
2718 emitGEPOperation(BB, BB->end(), I.getOperand(0), I.op_begin()+1, I.op_end(),
2719 outputReg, false, 0, 0);
2720}
2721
2722/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
2723/// constant expression GEP support.
2724///
2725void ISel::emitGEPOperation(MachineBasicBlock *MBB,
2726 MachineBasicBlock::iterator IP,
2727 Value *Src, User::op_iterator IdxBegin,
2728 User::op_iterator IdxEnd, unsigned TargetReg,
2729 bool GEPIsFolded, ConstantSInt **RemainderPtr,
2730 unsigned *PendingAddReg) {
2731 const TargetData &TD = TM.getTargetData();
2732 const Type *Ty = Src->getType();
2733 unsigned basePtrReg = getReg(Src, MBB, IP);
2734 int64_t constValue = 0;
2735
2736 // Record the operations to emit the GEP in a vector so that we can emit them
2737 // after having analyzed the entire instruction.
2738 std::vector<CollapsedGepOp> ops;
2739
2740 // GEPs have zero or more indices; we must perform a struct access
2741 // or array access for each one.
2742 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
2743 ++oi) {
2744 Value *idx = *oi;
2745 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2746 // It's a struct access. idx is the index into the structure,
2747 // which names the field. Use the TargetData structure to
2748 // pick out what the layout of the structure is in memory.
2749 // Use the (constant) structure index's value to find the
2750 // right byte offset from the StructLayout class's list of
2751 // structure member offsets.
2752 unsigned fieldIndex = cast<ConstantUInt>(idx)->getValue();
2753 unsigned memberOffset =
2754 TD.getStructLayout(StTy)->MemberOffsets[fieldIndex];
2755
2756 // StructType member offsets are always constant values. Add it to the
2757 // running total.
2758 constValue += memberOffset;
2759
2760 // The next type is the member of the structure selected by the
2761 // index.
2762 Ty = StTy->getElementType (fieldIndex);
2763 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
2764 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
2765 // operand. Handle this case directly now...
2766 if (CastInst *CI = dyn_cast<CastInst>(idx))
2767 if (CI->getOperand(0)->getType() == Type::IntTy ||
2768 CI->getOperand(0)->getType() == Type::UIntTy)
2769 idx = CI->getOperand(0);
2770
2771 // It's an array or pointer access: [ArraySize x ElementType].
2772 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
2773 // must find the size of the pointed-to type (Not coincidentally, the next
2774 // type is the type of the elements in the array).
2775 Ty = SqTy->getElementType();
2776 unsigned elementSize = TD.getTypeSize(Ty);
2777
2778 if (ConstantInt *C = dyn_cast<ConstantInt>(idx)) {
2779 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(C))
2780 constValue += CS->getValue() * elementSize;
2781 else if (ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
2782 constValue += CU->getValue() * elementSize;
2783 else
2784 assert(0 && "Invalid ConstantInt GEP index type!");
2785 } else {
2786 // Push current gep state to this point as an add
2787 ops.push_back(CollapsedGepOp(false, 0,
2788 ConstantSInt::get(Type::IntTy,constValue)));
2789
2790 // Push multiply gep op and reset constant value
2791 ops.push_back(CollapsedGepOp(true, idx,
2792 ConstantSInt::get(Type::IntTy, elementSize)));
2793
2794 constValue = 0;
2795 }
2796 }
2797 }
2798 // Emit instructions for all the collapsed ops
2799 bool pendingAdd = false;
2800 unsigned pendingAddReg = 0;
2801
2802 for(std::vector<CollapsedGepOp>::iterator cgo_i = ops.begin(),
2803 cgo_e = ops.end(); cgo_i != cgo_e; ++cgo_i) {
2804 CollapsedGepOp& cgo = *cgo_i;
2805 unsigned nextBasePtrReg = makeAnotherReg(Type::IntTy);
2806
2807 // If we didn't emit an add last time through the loop, we need to now so
2808 // that the base reg is updated appropriately.
2809 if (pendingAdd) {
2810 assert(pendingAddReg != 0 && "Uninitialized register in pending add!");
2811 BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg)
2812 .addReg(pendingAddReg);
2813 basePtrReg = nextBasePtrReg;
2814 nextBasePtrReg = makeAnotherReg(Type::IntTy);
2815 pendingAddReg = 0;
2816 pendingAdd = false;
2817 }
2818
2819 if (cgo.isMul) {
2820 // We know the elementSize is a constant, so we can emit a constant mul
2821 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2822 doMultiplyConst(MBB, IP, nextBasePtrReg, cgo.index, cgo.size);
2823 pendingAddReg = basePtrReg;
2824 pendingAdd = true;
2825 } else {
2826 // Try and generate an immediate addition if possible
2827 if (cgo.size->isNullValue()) {
2828 BuildMI(*MBB, IP, PPC::OR, 2, nextBasePtrReg).addReg(basePtrReg)
2829 .addReg(basePtrReg);
2830 } else if (canUseAsImmediateForOpcode(cgo.size, 0)) {
2831 BuildMI(*MBB, IP, PPC::ADDI, 2, nextBasePtrReg).addReg(basePtrReg)
2832 .addSImm(cgo.size->getValue());
2833 } else {
2834 unsigned Op1r = getReg(cgo.size, MBB, IP);
2835 BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg)
2836 .addReg(Op1r);
2837 }
2838 }
2839
2840 basePtrReg = nextBasePtrReg;
2841 }
2842 // Add the current base register plus any accumulated constant value
2843 ConstantSInt *remainder = ConstantSInt::get(Type::IntTy, constValue);
2844
2845 // If we are emitting this during a fold, copy the current base register to
2846 // the target, and save the current constant offset so the folding load or
2847 // store can try and use it as an immediate.
2848 if (GEPIsFolded) {
2849 // If this is a folded GEP and the last element was an index, then we need
2850 // to do some extra work to turn a shift/add/stw into a shift/stwx
2851 if (pendingAdd && 0 == remainder->getValue()) {
2852 assert(pendingAddReg != 0 && "Uninitialized register in pending add!");
2853 *PendingAddReg = pendingAddReg;
2854 } else {
2855 *PendingAddReg = 0;
2856 if (pendingAdd) {
2857 unsigned nextBasePtrReg = makeAnotherReg(Type::IntTy);
2858 assert(pendingAddReg != 0 && "Uninitialized register in pending add!");
2859 BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg)
2860 .addReg(pendingAddReg);
2861 basePtrReg = nextBasePtrReg;
2862 }
2863 }
2864 BuildMI (*MBB, IP, PPC::OR, 2, TargetReg).addReg(basePtrReg)
2865 .addReg(basePtrReg);
2866 *RemainderPtr = remainder;
2867 return;
2868 }
2869
2870 // If we still have a pending add at this point, emit it now
2871 if (pendingAdd) {
2872 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2873 BuildMI(*MBB, IP, PPC::ADD, 2, TmpReg).addReg(pendingAddReg)
2874 .addReg(basePtrReg);
2875 basePtrReg = TmpReg;
2876 }
2877
2878 // After we have processed all the indices, the result is left in
2879 // basePtrReg. Move it to the register where we were expected to
2880 // put the answer.
2881 if (remainder->isNullValue()) {
2882 BuildMI (*MBB, IP, PPC::OR, 2, TargetReg).addReg(basePtrReg)
2883 .addReg(basePtrReg);
2884 } else if (canUseAsImmediateForOpcode(remainder, 0)) {
2885 BuildMI(*MBB, IP, PPC::ADDI, 2, TargetReg).addReg(basePtrReg)
2886 .addSImm(remainder->getValue());
2887 } else {
2888 unsigned Op1r = getReg(remainder, MBB, IP);
2889 BuildMI(*MBB, IP, PPC::ADD, 2, TargetReg).addReg(basePtrReg).addReg(Op1r);
2890 }
2891}
2892
2893/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2894/// frame manager, otherwise do it the hard way.
2895///
2896void ISel::visitAllocaInst(AllocaInst &I) {
2897 // If this is a fixed size alloca in the entry block for the function, we
2898 // statically stack allocate the space, so we don't need to do anything here.
2899 //
2900 if (dyn_castFixedAlloca(&I)) return;
2901
2902 // Find the data size of the alloca inst's getAllocatedType.
2903 const Type *Ty = I.getAllocatedType();
2904 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2905
2906 // Create a register to hold the temporary result of multiplying the type size
2907 // constant by the variable amount.
2908 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2909
2910 // TotalSizeReg = mul <numelements>, <TypeSize>
2911 MachineBasicBlock::iterator MBBI = BB->end();
2912 ConstantUInt *CUI = ConstantUInt::get(Type::UIntTy, TySize);
2913 doMultiplyConst(BB, MBBI, TotalSizeReg, I.getArraySize(), CUI);
2914
2915 // AddedSize = add <TotalSizeReg>, 15
2916 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2917 BuildMI(BB, PPC::ADDI, 2, AddedSizeReg).addReg(TotalSizeReg).addSImm(15);
2918
2919 // AlignedSize = and <AddedSize>, ~15
2920 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2921 BuildMI(BB, PPC::RLWINM, 4, AlignedSize).addReg(AddedSizeReg).addImm(0)
2922 .addImm(0).addImm(27);
2923
2924 // Subtract size from stack pointer, thereby allocating some space.
2925 BuildMI(BB, PPC::SUB, 2, PPC::R1).addReg(PPC::R1).addReg(AlignedSize);
2926
2927 // Put a pointer to the space into the result register, by copying
2928 // the stack pointer.
2929 BuildMI(BB, PPC::OR, 2, getReg(I)).addReg(PPC::R1).addReg(PPC::R1);
2930
2931 // Inform the Frame Information that we have just allocated a variable-sized
2932 // object.
2933 F->getFrameInfo()->CreateVariableSizedObject();
2934}
2935
2936/// visitMallocInst - Malloc instructions are code generated into direct calls
2937/// to the library malloc.
2938///
2939void ISel::visitMallocInst(MallocInst &I) {
2940 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2941 unsigned Arg;
2942
2943 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2944 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2945 } else {
2946 Arg = makeAnotherReg(Type::UIntTy);
2947 MachineBasicBlock::iterator MBBI = BB->end();
2948 ConstantUInt *CUI = ConstantUInt::get(Type::UIntTy, AllocSize);
2949 doMultiplyConst(BB, MBBI, Arg, I.getOperand(0), CUI);
2950 }
2951
2952 std::vector<ValueRecord> Args;
2953 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2954 MachineInstr *TheCall =
2955 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(mallocFn, true);
2956 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002957}
2958
2959
2960/// visitFreeInst - Free instructions are code gen'd to call the free libc
2961/// function.
2962///
2963void ISel::visitFreeInst(FreeInst &I) {
2964 std::vector<ValueRecord> Args;
2965 Args.push_back(ValueRecord(I.getOperand(0)));
2966 MachineInstr *TheCall =
2967 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(freeFn, true);
2968 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args, false);
Misha Brukmanca9309f2004-08-11 23:42:15 +00002969}
2970
2971/// createPPC64ISelSimple - This pass converts an LLVM function into a machine
2972/// code representation is a very simple peep-hole fashion.
2973///
2974FunctionPass *llvm::createPPC64ISelSimple(TargetMachine &TM) {
2975 return new ISel(TM);
2976}