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