Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1 | //===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===// |
| 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 | // This file defines a simple peephole instruction selector for the V8 target |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SparcV8.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 15 | #include "SparcV8InstrInfo.h" |
Brian Gaeke | 74dfcf1 | 2004-09-02 02:37:43 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 3048373 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 28 | #include "llvm/Support/InstVisitor.h" |
| 29 | #include "llvm/Support/CFG.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> { |
| 34 | TargetMachine &TM; |
| 35 | MachineFunction *F; // The function we are compiling into |
| 36 | MachineBasicBlock *BB; // The current MBB we are compiling |
Brian Gaeke | d90282d | 2004-11-19 20:57:24 +0000 | [diff] [blame^] | 37 | int VarArgsOffset; // Offset from fp for start of varargs area |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 38 | |
| 39 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 40 | |
| 41 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 42 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 43 | |
| 44 | V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {} |
| 45 | |
| 46 | /// runOnFunction - Top level implementation of instruction selection for |
| 47 | /// the entire function. |
| 48 | /// |
| 49 | bool runOnFunction(Function &Fn); |
| 50 | |
| 51 | virtual const char *getPassName() const { |
| 52 | return "SparcV8 Simple Instruction Selection"; |
| 53 | } |
| 54 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 55 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 56 | /// constant expression GEP support. |
| 57 | /// |
| 58 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 59 | Value *Src, User::op_iterator IdxBegin, |
| 60 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 61 | |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 62 | /// emitCastOperation - Common code shared between visitCastInst and |
| 63 | /// constant expression cast support. |
| 64 | /// |
| 65 | void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP, |
| 66 | Value *Src, const Type *DestTy, unsigned TargetReg); |
| 67 | |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 68 | /// emitIntegerCast, emitFPToIntegerCast - Helper methods for |
| 69 | /// emitCastOperation. |
| 70 | /// |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 71 | unsigned emitIntegerCast (MachineBasicBlock *BB, |
| 72 | MachineBasicBlock::iterator IP, |
| 73 | const Type *oldTy, unsigned SrcReg, |
| 74 | const Type *newTy, unsigned DestReg); |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 75 | void emitFPToIntegerCast (MachineBasicBlock *BB, |
| 76 | MachineBasicBlock::iterator IP, const Type *oldTy, |
| 77 | unsigned SrcReg, const Type *newTy, |
| 78 | unsigned DestReg); |
| 79 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 80 | /// visitBasicBlock - This method is called when we are visiting a new basic |
| 81 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 82 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 83 | /// instructions will be invoked for all instructions in the basic block. |
| 84 | /// |
| 85 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
| 86 | BB = MBBMap[&LLVM_BB]; |
| 87 | } |
| 88 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 89 | void visitBinaryOperator(Instruction &I); |
Brian Gaeke | d6a1053 | 2004-06-15 21:09:46 +0000 | [diff] [blame] | 90 | void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); } |
Misha Brukman | ea09126 | 2004-06-30 21:47:40 +0000 | [diff] [blame] | 91 | void visitSetCondInst(SetCondInst &I); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 92 | void visitCallInst(CallInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 93 | void visitReturnInst(ReturnInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 94 | void visitBranchInst(BranchInst &I); |
Chris Lattner | d14d5b4 | 2004-10-17 02:42:42 +0000 | [diff] [blame] | 95 | void visitUnreachableInst(UnreachableInst &I) {} |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 96 | void visitCastInst(CastInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 97 | void visitLoadInst(LoadInst &I); |
| 98 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 99 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
| 100 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 101 | void visitAllocaInst(AllocaInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 103 | void visitInstruction(Instruction &I) { |
| 104 | std::cerr << "Unhandled instruction: " << I; |
| 105 | abort(); |
| 106 | } |
| 107 | |
| 108 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 109 | /// function, lowering any calls to unknown intrinsic functions into the |
| 110 | /// equivalent LLVM code. |
| 111 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 112 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 113 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 114 | void LoadArgumentsToVirtualRegs(Function *F); |
| 115 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 116 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 117 | /// because we have to generate our sources into the source basic blocks, |
| 118 | /// not the current one. |
| 119 | /// |
| 120 | void SelectPHINodes(); |
| 121 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 122 | /// copyConstantToRegister - Output the instructions required to put the |
| 123 | /// specified constant into the specified register. |
| 124 | /// |
| 125 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 126 | MachineBasicBlock::iterator IP, |
| 127 | Constant *C, unsigned R); |
| 128 | |
| 129 | /// makeAnotherReg - This method returns the next register number we haven't |
| 130 | /// yet used. |
| 131 | /// |
| 132 | /// Long values are handled somewhat specially. They are always allocated |
| 133 | /// as pairs of 32 bit integer values. The register number returned is the |
| 134 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 135 | /// of the long value. |
| 136 | /// |
| 137 | unsigned makeAnotherReg(const Type *Ty) { |
| 138 | assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) && |
| 139 | "Current target doesn't have SparcV8 reg info??"); |
| 140 | const SparcV8RegisterInfo *MRI = |
| 141 | static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()); |
| 142 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 143 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 144 | // Create the lower part |
| 145 | F->getSSARegMap()->createVirtualRegister(RC); |
| 146 | // Create the upper part. |
| 147 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 148 | } |
| 149 | |
| 150 | // Add the mapping of regnumber => reg class to MachineFunction |
| 151 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 152 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 153 | } |
| 154 | |
| 155 | unsigned getReg(Value &V) { return getReg (&V); } // allow refs. |
| 156 | unsigned getReg(Value *V) { |
| 157 | // Just append to the end of the current bb. |
| 158 | MachineBasicBlock::iterator It = BB->end(); |
| 159 | return getReg(V, BB, It); |
| 160 | } |
| 161 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 162 | MachineBasicBlock::iterator IPt) { |
| 163 | unsigned &Reg = RegMap[V]; |
| 164 | if (Reg == 0) { |
| 165 | Reg = makeAnotherReg(V->getType()); |
| 166 | RegMap[V] = Reg; |
| 167 | } |
| 168 | // If this operand is a constant, emit the code to copy the constant into |
| 169 | // the register here... |
| 170 | // |
| 171 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 172 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 173 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
| 174 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 175 | // Move the address of the global into the register |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 176 | unsigned TmpReg = makeAnotherReg(V->getType()); |
| 177 | BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV); |
| 178 | BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg) |
| 179 | .addGlobalAddress (GV); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 180 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 181 | } |
| 182 | |
| 183 | return Reg; |
| 184 | } |
| 185 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 186 | }; |
| 187 | } |
| 188 | |
| 189 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 190 | return new V8ISel(TM); |
| 191 | } |
| 192 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 193 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 194 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | static TypeClass getClass (const Type *T) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 198 | switch (T->getTypeID()) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 199 | case Type::UByteTyID: case Type::SByteTyID: return cByte; |
| 200 | case Type::UShortTyID: case Type::ShortTyID: return cShort; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 201 | case Type::PointerTyID: |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 202 | case Type::UIntTyID: case Type::IntTyID: return cInt; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 203 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 204 | case Type::FloatTyID: return cFloat; |
| 205 | case Type::DoubleTyID: return cDouble; |
| 206 | default: |
| 207 | assert (0 && "Type of unknown class passed to getClass?"); |
| 208 | return cByte; |
| 209 | } |
| 210 | } |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 212 | static TypeClass getClassB(const Type *T) { |
| 213 | if (T == Type::BoolTy) return cByte; |
| 214 | return getClass(T); |
| 215 | } |
| 216 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 217 | /// copyConstantToRegister - Output the instructions required to put the |
| 218 | /// specified constant into the specified register. |
| 219 | /// |
| 220 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 221 | MachineBasicBlock::iterator IP, |
| 222 | Constant *C, unsigned R) { |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 223 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 224 | switch (CE->getOpcode()) { |
| 225 | case Instruction::GetElementPtr: |
| 226 | emitGEPOperation(MBB, IP, CE->getOperand(0), |
| 227 | CE->op_begin()+1, CE->op_end(), R); |
| 228 | return; |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 229 | case Instruction::Cast: |
| 230 | emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R); |
| 231 | return; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 232 | default: |
| 233 | std::cerr << "Copying this constant expr not yet handled: " << *CE; |
| 234 | abort(); |
| 235 | } |
Chris Lattner | d14d5b4 | 2004-10-17 02:42:42 +0000 | [diff] [blame] | 236 | } else if (isa<UndefValue>(C)) { |
| 237 | BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R); |
| 238 | if (getClassB (C->getType ()) == cLong) |
| 239 | BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R+1); |
| 240 | return; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 243 | if (C->getType()->isIntegral ()) { |
| 244 | uint64_t Val; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 245 | unsigned Class = getClassB (C->getType ()); |
| 246 | if (Class == cLong) { |
| 247 | unsigned TmpReg = makeAnotherReg (Type::IntTy); |
| 248 | unsigned TmpReg2 = makeAnotherReg (Type::IntTy); |
| 249 | // Copy the value into the register pair. |
| 250 | // R = top(more-significant) half, R+1 = bottom(less-significant) half |
| 251 | uint64_t Val = cast<ConstantInt>(C)->getRawValue(); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 252 | copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy, |
| 253 | Val >> 32), R); |
| 254 | copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy, |
| 255 | Val & 0xffffffffU), R+1); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
| 259 | assert(Class <= cInt && "Type not handled yet!"); |
| 260 | |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 261 | if (C->getType() == Type::BoolTy) { |
| 262 | Val = (C == ConstantBool::True); |
| 263 | } else { |
Brian Gaeke | 13dc433 | 2004-06-24 09:17:47 +0000 | [diff] [blame] | 264 | ConstantInt *CI = cast<ConstantInt> (C); |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 265 | Val = CI->getRawValue (); |
| 266 | } |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 267 | switch (Class) { |
Brian Gaeke | 13dc433 | 2004-06-24 09:17:47 +0000 | [diff] [blame] | 268 | case cByte: Val = (int8_t) Val; break; |
| 269 | case cShort: Val = (int16_t) Val; break; |
| 270 | case cInt: Val = (int32_t) Val; break; |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 271 | default: |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 272 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 273 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 274 | return; |
| 275 | } |
Brian Gaeke | 13dc433 | 2004-06-24 09:17:47 +0000 | [diff] [blame] | 276 | if (Val == 0) { |
| 277 | BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0); |
| 278 | } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) { |
| 279 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val); |
| 280 | } else { |
| 281 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
| 282 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg) |
| 283 | .addSImm (((uint32_t) Val) >> 10); |
| 284 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 285 | .addSImm (((uint32_t) Val) & 0x03ff); |
| 286 | return; |
| 287 | } |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 288 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 289 | // We need to spill the constant to memory... |
| 290 | MachineConstantPool *CP = F->getConstantPool(); |
| 291 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 292 | const Type *Ty = CFP->getType(); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 293 | unsigned TmpReg = makeAnotherReg (Type::UIntTy); |
| 294 | unsigned AddrReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 295 | |
| 296 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 297 | unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 298 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI); |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 299 | BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg) |
| 300 | .addConstantPoolIndex (CPI); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 301 | BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 302 | } else if (isa<ConstantPointerNull>(C)) { |
| 303 | // Copy zero (null pointer) to the register. |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 304 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 305 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 306 | // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize |
| 307 | // that SETHI %reg,global == SETHI %reg,%hi(global) and |
| 308 | // OR %reg,global,%reg == OR %reg,%lo(global),%reg. |
| 309 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 310 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV); |
| 311 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 312 | } else { |
| 313 | std::cerr << "Offending constant: " << *C << "\n"; |
| 314 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 315 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 317 | |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 318 | void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) { |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 319 | static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2, |
| 320 | V8::I3, V8::I4, V8::I5 }; |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 321 | |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 322 | // Add IMPLICIT_DEFs of input regs. |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 323 | unsigned ArgNo = 0; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 324 | for (Function::aiterator I = LF->abegin(), E = LF->aend(); |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 325 | I != E && ArgNo < 6; ++I, ++ArgNo) { |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 326 | switch (getClassB(I->getType())) { |
| 327 | case cByte: |
| 328 | case cShort: |
| 329 | case cInt: |
| 330 | case cFloat: |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 331 | BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 332 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 333 | case cDouble: |
| 334 | case cLong: |
| 335 | // Double and Long use register pairs. |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 336 | BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]); |
| 337 | ++ArgNo; |
| 338 | if (ArgNo < 6) |
| 339 | BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 340 | break; |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 341 | default: |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 342 | assert (0 && "type not handled"); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 343 | return; |
| 344 | } |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 347 | const unsigned *IAREnd = &IncomingArgRegs[6]; |
| 348 | const unsigned *IAR = &IncomingArgRegs[0]; |
| 349 | unsigned ArgOffset = 68; |
Brian Gaeke | 4e459c4 | 2004-11-19 20:31:08 +0000 | [diff] [blame] | 350 | |
| 351 | // Store registers onto stack if this is a varargs function. |
| 352 | // FIXME: This doesn't really pertain to "loading arguments into |
| 353 | // virtual registers", so it's not clear that it really belongs here. |
| 354 | // FIXME: We could avoid storing any args onto the stack that don't |
| 355 | // need to be in memory, because they come before the ellipsis in the |
| 356 | // parameter list (and thus could never be accessed through va_arg). |
| 357 | if (LF->getFunctionType ()->isVarArg ()) { |
| 358 | for (unsigned i = 0; i < 6; ++i) { |
| 359 | int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset); |
| 360 | assert (IAR != IAREnd |
| 361 | && "About to dereference past end of IncomingArgRegs"); |
| 362 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++); |
| 363 | ArgOffset += 4; |
| 364 | } |
| 365 | // Reset the pointers now that we're done. |
| 366 | ArgOffset = 68; |
| 367 | IAR = &IncomingArgRegs[0]; |
| 368 | } |
| 369 | |
| 370 | // Copy args out of their incoming hard regs or stack slots into virtual regs. |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 371 | for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E; ++I) { |
| 372 | Argument &A = *I; |
| 373 | unsigned ArgReg = getReg (A); |
| 374 | if (getClassB (A.getType ()) < cLong) { |
| 375 | // Get it out of the incoming arg register |
| 376 | if (ArgOffset < 92) { |
| 377 | assert (IAR != IAREnd |
| 378 | && "About to dereference past end of IncomingArgRegs"); |
| 379 | BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++); |
| 380 | } else { |
| 381 | int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset); |
| 382 | BuildMI (BB, V8::LD, 3, ArgReg).addFrameIndex (FI).addSImm (0); |
| 383 | } |
| 384 | ArgOffset += 4; |
| 385 | } else if (getClassB (A.getType ()) == cFloat) { |
| 386 | if (ArgOffset < 92) { |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 387 | // Single-fp args are passed in integer registers; go through |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 388 | // memory to get them out of integer registers and back into fp. (Bleh!) |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 389 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
| 390 | int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 391 | assert (IAR != IAREnd |
| 392 | && "About to dereference past end of IncomingArgRegs"); |
| 393 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++); |
| 394 | BuildMI (BB, V8::LDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0); |
| 395 | } else { |
| 396 | int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset); |
| 397 | BuildMI (BB, V8::LDFri, 3, ArgReg).addFrameIndex (FI).addSImm (0); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 398 | } |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 399 | ArgOffset += 4; |
| 400 | } else if (getClassB (A.getType ()) == cDouble) { |
| 401 | // Double-fp args are passed in pairs of integer registers; go through |
| 402 | // memory to get them out of integer registers and back into fp. (Bleh!) |
| 403 | // We'd like to 'ldd' these right out of the incoming-args area, |
| 404 | // but it might not be 8-byte aligned (e.g., call x(int x, double d)). |
| 405 | unsigned DblAlign = TM.getTargetData().getDoubleAlignment(); |
| 406 | int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign); |
| 407 | if (ArgOffset < 92 && IAR != IAREnd) { |
| 408 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++); |
| 409 | } else { |
| 410 | unsigned TempReg = makeAnotherReg (Type::IntTy); |
| 411 | BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0); |
| 412 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg); |
Brian Gaeke | 6672f86 | 2004-09-30 19:44:32 +0000 | [diff] [blame] | 413 | } |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 414 | ArgOffset += 4; |
| 415 | if (ArgOffset < 92 && IAR != IAREnd) { |
| 416 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (*IAR++); |
| 417 | } else { |
| 418 | unsigned TempReg = makeAnotherReg (Type::IntTy); |
| 419 | BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4); |
| 420 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 421 | } |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 422 | ArgOffset += 4; |
| 423 | BuildMI (BB, V8::LDDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0); |
| 424 | } else if (getClassB (A.getType ()) == cLong) { |
| 425 | // do the first half... |
| 426 | if (ArgOffset < 92) { |
| 427 | assert (IAR != IAREnd |
| 428 | && "About to dereference past end of IncomingArgRegs"); |
| 429 | BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++); |
| 430 | } else { |
| 431 | int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset); |
| 432 | BuildMI (BB, V8::LD, 2, ArgReg).addFrameIndex (FI).addSImm (0); |
| 433 | } |
| 434 | ArgOffset += 4; |
| 435 | // ...then do the second half |
| 436 | if (ArgOffset < 92) { |
| 437 | assert (IAR != IAREnd |
| 438 | && "About to dereference past end of IncomingArgRegs"); |
| 439 | BuildMI (BB, V8::ORrr, 2, ArgReg+1).addReg (V8::G0).addReg (*IAR++); |
| 440 | } else { |
| 441 | int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset); |
| 442 | BuildMI (BB, V8::LD, 2, ArgReg+1).addFrameIndex (FI).addSImm (0); |
| 443 | } |
| 444 | ArgOffset += 4; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 445 | } else { |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 446 | assert (0 && "Unknown class?!"); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 447 | } |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 448 | } |
Brian Gaeke | d90282d | 2004-11-19 20:57:24 +0000 | [diff] [blame^] | 449 | |
| 450 | // If the function takes variable number of arguments, remember the fp |
| 451 | // offset for the start of the first vararg value... this is used to expand |
| 452 | // llvm.va_start. |
| 453 | if (LF->getFunctionType ()->isVarArg ()) |
| 454 | VarArgsOffset = ArgOffset; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 457 | void V8ISel::SelectPHINodes() { |
| 458 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 459 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 460 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 461 | const BasicBlock *BB = I; |
| 462 | MachineBasicBlock &MBB = *MBBMap[I]; |
| 463 | |
| 464 | // Loop over all of the PHI nodes in the LLVM basic block... |
| 465 | MachineBasicBlock::iterator PHIInsertPoint = MBB.begin(); |
| 466 | for (BasicBlock::const_iterator I = BB->begin(); |
| 467 | PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) { |
| 468 | |
| 469 | // Create a new machine instr PHI node, and insert it. |
| 470 | unsigned PHIReg = getReg(*PN); |
| 471 | MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint, |
| 472 | V8::PHI, PN->getNumOperands(), PHIReg); |
| 473 | |
| 474 | MachineInstr *LongPhiMI = 0; |
| 475 | if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) |
| 476 | LongPhiMI = BuildMI(MBB, PHIInsertPoint, |
| 477 | V8::PHI, PN->getNumOperands(), PHIReg+1); |
| 478 | |
| 479 | // PHIValues - Map of blocks to incoming virtual registers. We use this |
| 480 | // so that we only initialize one incoming value for a particular block, |
| 481 | // even if the block has multiple entries in the PHI node. |
| 482 | // |
| 483 | std::map<MachineBasicBlock*, unsigned> PHIValues; |
| 484 | |
| 485 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 486 | MachineBasicBlock *PredMBB = 0; |
| 487 | for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (), |
| 488 | PE = MBB.pred_end (); PI != PE; ++PI) |
| 489 | if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) { |
| 490 | PredMBB = *PI; |
| 491 | break; |
| 492 | } |
| 493 | assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi"); |
| 494 | |
| 495 | unsigned ValReg; |
| 496 | std::map<MachineBasicBlock*, unsigned>::iterator EntryIt = |
| 497 | PHIValues.lower_bound(PredMBB); |
| 498 | |
| 499 | if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { |
| 500 | // We already inserted an initialization of the register for this |
| 501 | // predecessor. Recycle it. |
| 502 | ValReg = EntryIt->second; |
| 503 | |
| 504 | } else { |
| 505 | // Get the incoming value into a virtual register. |
| 506 | // |
| 507 | Value *Val = PN->getIncomingValue(i); |
| 508 | |
| 509 | // If this is a constant or GlobalValue, we may have to insert code |
| 510 | // into the basic block to compute it into a virtual register. |
| 511 | if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) || |
| 512 | isa<GlobalValue>(Val)) { |
| 513 | // Simple constants get emitted at the end of the basic block, |
| 514 | // before any terminator instructions. We "know" that the code to |
| 515 | // move a constant into a register will never clobber any flags. |
| 516 | ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator()); |
| 517 | } else { |
| 518 | // Because we don't want to clobber any values which might be in |
| 519 | // physical registers with the computation of this constant (which |
| 520 | // might be arbitrarily complex if it is a constant expression), |
| 521 | // just insert the computation at the top of the basic block. |
| 522 | MachineBasicBlock::iterator PI = PredMBB->begin(); |
| 523 | |
| 524 | // Skip over any PHI nodes though! |
| 525 | while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI) |
| 526 | ++PI; |
| 527 | |
| 528 | ValReg = getReg(Val, PredMBB, PI); |
| 529 | } |
| 530 | |
| 531 | // Remember that we inserted a value for this PHI for this predecessor |
| 532 | PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); |
| 533 | } |
| 534 | |
| 535 | PhiMI->addRegOperand(ValReg); |
| 536 | PhiMI->addMachineBasicBlockOperand(PredMBB); |
| 537 | if (LongPhiMI) { |
| 538 | LongPhiMI->addRegOperand(ValReg+1); |
| 539 | LongPhiMI->addMachineBasicBlockOperand(PredMBB); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // Now that we emitted all of the incoming values for the PHI node, make |
| 544 | // sure to reposition the InsertPoint after the PHI that we just added. |
| 545 | // This is needed because we might have inserted a constant into this |
| 546 | // block, right after the PHI's which is before the old insert point! |
| 547 | PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI; |
| 548 | ++PHIInsertPoint; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 553 | bool V8ISel::runOnFunction(Function &Fn) { |
| 554 | // First pass over the function, lower any unknown intrinsic functions |
| 555 | // with the IntrinsicLowering class. |
| 556 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 557 | |
| 558 | F = &MachineFunction::construct(&Fn, TM); |
| 559 | |
| 560 | // Create all of the machine basic blocks for the function... |
| 561 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 562 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 563 | |
| 564 | BB = &F->front(); |
| 565 | |
| 566 | // Set up a frame object for the return address. This is used by the |
| 567 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 568 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 569 | |
| 570 | // Copy incoming arguments off of the stack and out of fixed registers. |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 571 | LoadArgumentsToVirtualRegs(&Fn); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 572 | |
| 573 | // Instruction select everything except PHI nodes |
| 574 | visit(Fn); |
| 575 | |
| 576 | // Select the PHI nodes |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 577 | SelectPHINodes(); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 578 | |
| 579 | RegMap.clear(); |
| 580 | MBBMap.clear(); |
| 581 | F = 0; |
| 582 | // We always build a machine code representation for the function |
| 583 | return true; |
| 584 | } |
| 585 | |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 586 | void V8ISel::visitCastInst(CastInst &I) { |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 587 | Value *Op = I.getOperand(0); |
| 588 | unsigned DestReg = getReg(I); |
| 589 | MachineBasicBlock::iterator MI = BB->end(); |
| 590 | emitCastOperation(BB, MI, Op, I.getType(), DestReg); |
| 591 | } |
| 592 | |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 593 | |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 594 | unsigned V8ISel::emitIntegerCast (MachineBasicBlock *BB, |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 595 | MachineBasicBlock::iterator IP, const Type *oldTy, |
| 596 | unsigned SrcReg, const Type *newTy, |
| 597 | unsigned DestReg) { |
| 598 | if (oldTy == newTy) { |
| 599 | // No-op cast - just emit a copy; assume the reg. allocator will zap it. |
| 600 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg(SrcReg); |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 601 | return SrcReg; |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 602 | } |
| 603 | // Emit left-shift, then right-shift to sign- or zero-extend. |
| 604 | unsigned TmpReg = makeAnotherReg (newTy); |
| 605 | unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy)); |
| 606 | BuildMI (*BB, IP, V8::SLLri, 2, TmpReg).addZImm (shiftWidth).addReg(SrcReg); |
| 607 | if (newTy->isSigned ()) { // sign-extend with SRA |
| 608 | BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg); |
| 609 | } else { // zero-extend with SRL |
| 610 | BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg); |
| 611 | } |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 612 | // Return the temp reg. in case this is one half of a cast to long. |
| 613 | return TmpReg; |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void V8ISel::emitFPToIntegerCast (MachineBasicBlock *BB, |
| 617 | MachineBasicBlock::iterator IP, |
| 618 | const Type *oldTy, unsigned SrcReg, |
| 619 | const Type *newTy, unsigned DestReg) { |
| 620 | unsigned FPCastOpcode, FPStoreOpcode, FPSize, FPAlign; |
| 621 | unsigned oldTyClass = getClassB(oldTy); |
| 622 | if (oldTyClass == cFloat) { |
| 623 | FPCastOpcode = V8::FSTOI; FPStoreOpcode = V8::STFri; FPSize = 4; |
| 624 | FPAlign = TM.getTargetData().getFloatAlignment(); |
| 625 | } else { // it's a double |
| 626 | FPCastOpcode = V8::FDTOI; FPStoreOpcode = V8::STDFri; FPSize = 8; |
| 627 | FPAlign = TM.getTargetData().getDoubleAlignment(); |
| 628 | } |
| 629 | unsigned TempReg = makeAnotherReg (oldTy); |
| 630 | BuildMI (*BB, IP, FPCastOpcode, 1, TempReg).addReg (SrcReg); |
| 631 | int FI = F->getFrameInfo()->CreateStackObject(FPSize, FPAlign); |
| 632 | BuildMI (*BB, IP, FPStoreOpcode, 3).addFrameIndex (FI).addSImm (0) |
| 633 | .addReg (TempReg); |
| 634 | unsigned TempReg2 = makeAnotherReg (newTy); |
| 635 | BuildMI (*BB, IP, V8::LD, 3, TempReg2).addFrameIndex (FI).addSImm (0); |
| 636 | emitIntegerCast (BB, IP, Type::IntTy, TempReg2, newTy, DestReg); |
| 637 | } |
| 638 | |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 639 | /// emitCastOperation - Common code shared between visitCastInst and constant |
| 640 | /// expression cast support. |
| 641 | /// |
| 642 | void V8ISel::emitCastOperation(MachineBasicBlock *BB, |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 643 | MachineBasicBlock::iterator IP, Value *Src, |
| 644 | const Type *DestTy, unsigned DestReg) { |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 645 | const Type *SrcTy = Src->getType(); |
| 646 | unsigned SrcClass = getClassB(SrcTy); |
| 647 | unsigned DestClass = getClassB(DestTy); |
| 648 | unsigned SrcReg = getReg(Src, BB, IP); |
| 649 | |
| 650 | const Type *oldTy = SrcTy; |
| 651 | const Type *newTy = DestTy; |
| 652 | unsigned oldTyClass = SrcClass; |
| 653 | unsigned newTyClass = DestClass; |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 654 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 655 | if (oldTyClass < cLong && newTyClass < cLong) { |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 656 | emitIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg); |
| 657 | } else switch (newTyClass) { |
| 658 | case cByte: |
| 659 | case cShort: |
| 660 | case cInt: |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 661 | switch (oldTyClass) { |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 662 | case cLong: |
| 663 | // Treat it like a cast from the lower half of the value. |
| 664 | emitIntegerCast (BB, IP, Type::IntTy, SrcReg+1, newTy, DestReg); |
| 665 | break; |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 666 | case cFloat: |
| 667 | case cDouble: |
| 668 | emitFPToIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg); |
| 669 | break; |
| 670 | default: goto not_yet; |
| 671 | } |
| 672 | return; |
| 673 | |
| 674 | case cFloat: |
| 675 | switch (oldTyClass) { |
| 676 | case cLong: goto not_yet; |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 677 | case cFloat: |
| 678 | BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg); |
| 679 | break; |
| 680 | case cDouble: |
| 681 | BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg); |
| 682 | break; |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 683 | default: { |
| 684 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 685 | // cast integer type to float. Store it to a stack slot and then load |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 686 | // it using ldf into a floating point register. then do fitos. |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 687 | unsigned TmpReg = makeAnotherReg (newTy); |
| 688 | int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
| 689 | BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0) |
| 690 | .addReg (SrcReg); |
| 691 | BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0); |
| 692 | BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg); |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 695 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 696 | return; |
| 697 | |
| 698 | case cDouble: |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 699 | switch (oldTyClass) { |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 700 | case cLong: goto not_yet; |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 701 | case cFloat: |
| 702 | BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg); |
| 703 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 704 | case cDouble: // use double move pseudo-instr |
| 705 | BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg); |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 706 | break; |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 707 | default: { |
| 708 | unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment(); |
| 709 | unsigned TmpReg = makeAnotherReg (newTy); |
| 710 | int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment); |
| 711 | BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0) |
| 712 | .addReg (SrcReg); |
| 713 | BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0); |
| 714 | BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg); |
| 715 | break; |
| 716 | } |
| 717 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 718 | return; |
| 719 | |
| 720 | case cLong: |
| 721 | switch (oldTyClass) { |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 722 | case cByte: |
| 723 | case cShort: |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 724 | case cInt: { |
| 725 | // Cast to (u)int in the bottom half, and sign(zero) extend in the top |
| 726 | // half. |
| 727 | const Type *OldHalfTy = oldTy->isSigned() ? Type::IntTy : Type::UIntTy; |
| 728 | const Type *NewHalfTy = newTy->isSigned() ? Type::IntTy : Type::UIntTy; |
| 729 | unsigned TempReg = emitIntegerCast (BB, IP, OldHalfTy, SrcReg, |
| 730 | NewHalfTy, DestReg+1); |
| 731 | if (newTy->isSigned ()) { |
| 732 | BuildMI (*BB, IP, V8::SRAri, 2, DestReg).addReg (TempReg) |
| 733 | .addZImm (31); |
| 734 | } else { |
| 735 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0) |
| 736 | .addReg (V8::G0); |
| 737 | } |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 738 | break; |
Brian Gaeke | a54df25 | 2004-11-19 18:48:10 +0000 | [diff] [blame] | 739 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 740 | case cLong: |
Brian Gaeke | 7c0afe0 | 2004-11-18 07:43:33 +0000 | [diff] [blame] | 741 | // Just copy both halves. |
Brian Gaeke | 2a9f539 | 2004-07-08 07:52:13 +0000 | [diff] [blame] | 742 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg); |
| 743 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0) |
| 744 | .addReg (SrcReg+1); |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 745 | break; |
| 746 | default: goto not_yet; |
Brian Gaeke | 2a9f539 | 2004-07-08 07:52:13 +0000 | [diff] [blame] | 747 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 748 | return; |
| 749 | |
| 750 | default: goto not_yet; |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 751 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 752 | return; |
| 753 | not_yet: |
| 754 | std::cerr << "Sorry, cast still unsupported: SrcTy = " << *SrcTy |
| 755 | << ", DestTy = " << *DestTy << "\n"; |
| 756 | abort (); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 759 | void V8ISel::visitLoadInst(LoadInst &I) { |
| 760 | unsigned DestReg = getReg (I); |
| 761 | unsigned PtrReg = getReg (I.getOperand (0)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 762 | switch (getClassB (I.getType ())) { |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 763 | case cByte: |
| 764 | if (I.getType ()->isSigned ()) |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 765 | BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 766 | else |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 767 | BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 768 | return; |
| 769 | case cShort: |
| 770 | if (I.getType ()->isSigned ()) |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 771 | BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 772 | else |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 773 | BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 774 | return; |
| 775 | case cInt: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 776 | BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 777 | return; |
| 778 | case cLong: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 779 | BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0); |
| 780 | BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4); |
| 781 | return; |
| 782 | case cFloat: |
| 783 | BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0); |
| 784 | return; |
| 785 | case cDouble: |
| 786 | BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 787 | return; |
| 788 | default: |
| 789 | std::cerr << "Load instruction not handled: " << I; |
| 790 | abort (); |
| 791 | return; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void V8ISel::visitStoreInst(StoreInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 796 | Value *SrcVal = I.getOperand (0); |
| 797 | unsigned SrcReg = getReg (SrcVal); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 798 | unsigned PtrReg = getReg (I.getOperand (1)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 799 | switch (getClassB (SrcVal->getType ())) { |
| 800 | case cByte: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 801 | BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 802 | return; |
| 803 | case cShort: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 804 | BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 805 | return; |
| 806 | case cInt: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 807 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 808 | return; |
| 809 | case cLong: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 810 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
| 811 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1); |
| 812 | return; |
| 813 | case cFloat: |
| 814 | BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
| 815 | return; |
| 816 | case cDouble: |
| 817 | BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 818 | return; |
| 819 | default: |
| 820 | std::cerr << "Store instruction not handled: " << I; |
| 821 | abort (); |
| 822 | return; |
| 823 | } |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 826 | void V8ISel::visitCallInst(CallInst &I) { |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 827 | MachineInstr *TheCall; |
| 828 | // Is it an intrinsic function call? |
| 829 | if (Function *F = I.getCalledFunction()) { |
| 830 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) { |
| 831 | visitIntrinsicCall(ID, I); // Special intrinsics are not handled here |
| 832 | return; |
| 833 | } |
| 834 | } |
| 835 | |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 836 | unsigned extraStack = 0; |
| 837 | // How much extra call stack will we need? |
| 838 | for (unsigned i = 7; i < I.getNumOperands (); ++i) { |
| 839 | switch (getClassB (I.getOperand (i)->getType ())) { |
| 840 | case cLong: extraStack += 8; break; |
| 841 | case cFloat: extraStack += 4; break; |
| 842 | case cDouble: extraStack += 8; break; |
| 843 | default: extraStack += 4; break; |
| 844 | } |
| 845 | } |
Brian Gaeke | 04fe747 | 2004-11-14 05:19:00 +0000 | [diff] [blame] | 846 | // Round up extra stack size to the nearest doubleword. |
| 847 | if (extraStack) { extraStack = (extraStack + 7) & ~7; } |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 848 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 849 | // Deal with args |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 850 | static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3, |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 851 | V8::O4, V8::O5 }; |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame] | 852 | const unsigned *OAREnd = &OutgoingArgRegs[6]; |
Brian Gaeke | 6931fd6 | 2004-11-04 00:27:04 +0000 | [diff] [blame] | 853 | const unsigned *OAR = &OutgoingArgRegs[0]; |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame] | 854 | unsigned ArgOffset = 68; |
Brian Gaeke | da9b366 | 2004-11-14 06:32:08 +0000 | [diff] [blame] | 855 | if (extraStack) BuildMI (BB, V8::ADJCALLSTACKDOWN, 1).addImm (extraStack); |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 856 | for (unsigned i = 1; i < I.getNumOperands (); ++i) { |
| 857 | unsigned ArgReg = getReg (I.getOperand (i)); |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame] | 858 | if (getClassB (I.getOperand (i)->getType ()) < cLong) { |
| 859 | // Schlep it over into the incoming arg register |
| 860 | if (ArgOffset < 92) { |
| 861 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 862 | BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 863 | } else { |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame] | 864 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 865 | } |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame] | 866 | ArgOffset += 4; |
| 867 | } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) { |
| 868 | if (ArgOffset < 92) { |
| 869 | // Single-fp args are passed in integer registers; go through |
| 870 | // memory to get them out of FP registers. (Bleh!) |
| 871 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
| 872 | int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
| 873 | BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg); |
| 874 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 875 | BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0); |
| 876 | } else { |
| 877 | BuildMI (BB, V8::STFri, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg); |
| 878 | } |
| 879 | ArgOffset += 4; |
| 880 | } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) { |
| 881 | // Double-fp args are passed in pairs of integer registers; go through |
| 882 | // memory to get them out of FP registers. (Bleh!) |
| 883 | // We'd like to 'std' these right onto the outgoing-args area, but it might |
| 884 | // not be 8-byte aligned (e.g., call x(int x, double d)). sigh. |
| 885 | unsigned DblAlign = TM.getTargetData().getDoubleAlignment(); |
| 886 | int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign); |
| 887 | BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg); |
| 888 | if (ArgOffset < 92 && OAR != OAREnd) { |
| 889 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 890 | BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0); |
| 891 | } else { |
| 892 | unsigned TempReg = makeAnotherReg (Type::IntTy); |
| 893 | BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0); |
| 894 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg); |
| 895 | } |
| 896 | ArgOffset += 4; |
| 897 | if (ArgOffset < 92 && OAR != OAREnd) { |
| 898 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 899 | BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (4); |
| 900 | } else { |
| 901 | unsigned TempReg = makeAnotherReg (Type::IntTy); |
| 902 | BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4); |
| 903 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg); |
| 904 | } |
| 905 | ArgOffset += 4; |
| 906 | } else if (getClassB (I.getOperand (i)->getType ()) == cLong) { |
| 907 | // do the first half... |
| 908 | if (ArgOffset < 92) { |
| 909 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 910 | BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg); |
| 911 | } else { |
| 912 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg); |
| 913 | } |
| 914 | ArgOffset += 4; |
| 915 | // ...then do the second half |
| 916 | if (ArgOffset < 92) { |
| 917 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 918 | BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg+1); |
| 919 | } else { |
| 920 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg+1); |
| 921 | } |
| 922 | ArgOffset += 4; |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 923 | } else { |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame] | 924 | assert (0 && "Unknown class?!"); |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 925 | } |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 926 | } |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 927 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 928 | // Emit call instruction |
| 929 | if (Function *F = I.getCalledFunction ()) { |
| 930 | BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true); |
| 931 | } else { // Emit an indirect call... |
| 932 | unsigned Reg = getReg (I.getCalledValue ()); |
| 933 | BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0); |
| 934 | } |
| 935 | |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 936 | if (extraStack) BuildMI (BB, V8::ADJCALLSTACKUP, 1).addImm (extraStack); |
| 937 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 938 | // Deal w/ return value: schlep it over into the destination register |
Brian Gaeke | e14e338 | 2004-06-15 20:06:32 +0000 | [diff] [blame] | 939 | if (I.getType () == Type::VoidTy) |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 940 | return; |
Brian Gaeke | e14e338 | 2004-06-15 20:06:32 +0000 | [diff] [blame] | 941 | unsigned DestReg = getReg (I); |
Brian Gaeke | 299b39d | 2004-10-10 20:34:17 +0000 | [diff] [blame] | 942 | switch (getClassB (I.getType ())) { |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 943 | case cByte: |
| 944 | case cShort: |
| 945 | case cInt: |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 946 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 947 | break; |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 948 | case cFloat: |
| 949 | BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0); |
| 950 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 951 | case cDouble: |
| 952 | BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0); |
| 953 | break; |
| 954 | case cLong: |
| 955 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 956 | BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1); |
| 957 | break; |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 958 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 959 | std::cerr << "Return type of call instruction not handled: " << I; |
| 960 | abort (); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 961 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 962 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 963 | |
| 964 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 965 | if (I.getNumOperands () == 1) { |
| 966 | unsigned RetValReg = getReg (I.getOperand (0)); |
Brian Gaeke | 299b39d | 2004-10-10 20:34:17 +0000 | [diff] [blame] | 967 | switch (getClassB (I.getOperand (0)->getType ())) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 968 | case cByte: |
| 969 | case cShort: |
| 970 | case cInt: |
| 971 | // Schlep it over into i0 (where it will become o0 after restore). |
| 972 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 973 | break; |
Brian Gaeke | f9a7546 | 2004-07-08 07:22:27 +0000 | [diff] [blame] | 974 | case cFloat: |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 975 | BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg); |
Brian Gaeke | f9a7546 | 2004-07-08 07:22:27 +0000 | [diff] [blame] | 976 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 977 | case cDouble: |
| 978 | BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 979 | break; |
Brian Gaeke | 2a9f539 | 2004-07-08 07:52:13 +0000 | [diff] [blame] | 980 | case cLong: |
| 981 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 982 | BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1); |
| 983 | break; |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 984 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 985 | std::cerr << "Return instruction of this type not handled: " << I; |
| 986 | abort (); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 987 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 988 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 989 | |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 990 | // Just emit a 'retl' instruction to return. |
| 991 | BuildMI(BB, V8::RETL, 0); |
| 992 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 995 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 996 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 997 | return I != BB->getParent()->end() ? &*I : 0; |
| 998 | } |
| 999 | |
| 1000 | /// visitBranchInst - Handles conditional and unconditional branches. |
| 1001 | /// |
| 1002 | void V8ISel::visitBranchInst(BranchInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1003 | BasicBlock *takenSucc = I.getSuccessor (0); |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 1004 | MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc]; |
| 1005 | BB->addSuccessor (takenSuccMBB); |
| 1006 | if (I.isConditional()) { // conditional branch |
| 1007 | BasicBlock *notTakenSucc = I.getSuccessor (1); |
| 1008 | MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc]; |
| 1009 | BB->addSuccessor (notTakenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1010 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 1011 | // CondReg=(<condition>); |
| 1012 | // If (CondReg==0) goto notTakenSuccMBB; |
| 1013 | unsigned CondReg = getReg (I.getCondition ()); |
| 1014 | BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg); |
| 1015 | BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1016 | } |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 1017 | // goto takenSuccMBB; |
| 1018 | BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 1022 | /// constant expression GEP support. |
| 1023 | /// |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 1024 | void V8ISel::emitGEPOperation (MachineBasicBlock *MBB, |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1025 | MachineBasicBlock::iterator IP, |
| 1026 | Value *Src, User::op_iterator IdxBegin, |
| 1027 | User::op_iterator IdxEnd, unsigned TargetReg) { |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 1028 | const TargetData &TD = TM.getTargetData (); |
| 1029 | const Type *Ty = Src->getType (); |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1030 | unsigned basePtrReg = getReg (Src, MBB, IP); |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 1031 | |
| 1032 | // GEPs have zero or more indices; we must perform a struct access |
| 1033 | // or array access for each one. |
| 1034 | for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe; |
| 1035 | ++oi) { |
| 1036 | Value *idx = *oi; |
| 1037 | unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy); |
| 1038 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 1039 | // It's a struct access. idx is the index into the structure, |
| 1040 | // which names the field. Use the TargetData structure to |
| 1041 | // pick out what the layout of the structure is in memory. |
| 1042 | // Use the (constant) structure index's value to find the |
| 1043 | // right byte offset from the StructLayout class's list of |
| 1044 | // structure member offsets. |
| 1045 | unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue (); |
| 1046 | unsigned memberOffset = |
| 1047 | TD.getStructLayout (StTy)->MemberOffsets[fieldIndex]; |
| 1048 | // Emit an ADD to add memberOffset to the basePtr. |
| 1049 | BuildMI (*MBB, IP, V8::ADDri, 2, |
| 1050 | nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset); |
| 1051 | // The next type is the member of the structure selected by the |
| 1052 | // index. |
| 1053 | Ty = StTy->getElementType (fieldIndex); |
| 1054 | } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) { |
| 1055 | // It's an array or pointer access: [ArraySize x ElementType]. |
| 1056 | // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we |
| 1057 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 1058 | // type is the type of the elements in the array). |
| 1059 | Ty = SqTy->getElementType (); |
| 1060 | unsigned elementSize = TD.getTypeSize (Ty); |
| 1061 | unsigned idxReg = getReg (idx, MBB, IP); |
| 1062 | unsigned OffsetReg = makeAnotherReg (Type::IntTy); |
| 1063 | unsigned elementSizeReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1064 | copyConstantToRegister (MBB, IP, |
| 1065 | ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg); |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 1066 | // Emit a SMUL to multiply the register holding the index by |
| 1067 | // elementSize, putting the result in OffsetReg. |
| 1068 | BuildMI (*MBB, IP, V8::SMULrr, 2, |
| 1069 | OffsetReg).addReg (elementSizeReg).addReg (idxReg); |
| 1070 | // Emit an ADD to add OffsetReg to the basePtr. |
| 1071 | BuildMI (*MBB, IP, V8::ADDrr, 2, |
| 1072 | nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg); |
| 1073 | } |
| 1074 | basePtrReg = nextBasePtrReg; |
| 1075 | } |
| 1076 | // After we have processed all the indices, the result is left in |
| 1077 | // basePtrReg. Move it to the register where we were expected to |
| 1078 | // put the answer. |
| 1079 | BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) { |
| 1083 | unsigned outputReg = getReg (I); |
| 1084 | emitGEPOperation (BB, BB->end (), I.getOperand (0), |
| 1085 | I.op_begin ()+1, I.op_end (), outputReg); |
| 1086 | } |
| 1087 | |
Brian Gaeke | d6a1053 | 2004-06-15 21:09:46 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 1089 | void V8ISel::visitBinaryOperator (Instruction &I) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1090 | unsigned DestReg = getReg (I); |
| 1091 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 1092 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 1093 | |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1094 | unsigned Class = getClassB (I.getType()); |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1095 | unsigned OpCase = ~0; |
| 1096 | |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1097 | if (Class > cLong) { |
| 1098 | switch (I.getOpcode ()) { |
| 1099 | case Instruction::Add: OpCase = 0; break; |
| 1100 | case Instruction::Sub: OpCase = 1; break; |
| 1101 | case Instruction::Mul: OpCase = 2; break; |
| 1102 | case Instruction::Div: OpCase = 3; break; |
| 1103 | default: visitInstruction (I); return; |
| 1104 | } |
| 1105 | static unsigned Opcodes[] = { V8::FADDS, V8::FADDD, |
| 1106 | V8::FSUBS, V8::FSUBD, |
| 1107 | V8::FMULS, V8::FMULD, |
| 1108 | V8::FDIVS, V8::FDIVD }; |
| 1109 | BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg) |
| 1110 | .addReg (Op0Reg).addReg (Op1Reg); |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | unsigned ResultReg = DestReg; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 1115 | if (Class != cInt && Class != cLong) |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1116 | ResultReg = makeAnotherReg (I.getType ()); |
| 1117 | |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 1118 | if (Class == cLong) { |
| 1119 | DEBUG (std::cerr << "Class = cLong\n"); |
| 1120 | DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n"); |
| 1121 | DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n"); |
| 1122 | DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n"); |
| 1123 | DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n"); |
| 1124 | } |
| 1125 | |
| 1126 | // FIXME: support long, ulong. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1127 | switch (I.getOpcode ()) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1128 | case Instruction::Add: OpCase = 0; break; |
| 1129 | case Instruction::Sub: OpCase = 1; break; |
| 1130 | case Instruction::Mul: OpCase = 2; break; |
| 1131 | case Instruction::And: OpCase = 3; break; |
| 1132 | case Instruction::Or: OpCase = 4; break; |
| 1133 | case Instruction::Xor: OpCase = 5; break; |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 1134 | case Instruction::Shl: OpCase = 6; break; |
| 1135 | case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1136 | |
| 1137 | case Instruction::Div: |
| 1138 | case Instruction::Rem: { |
| 1139 | unsigned Dest = ResultReg; |
| 1140 | if (I.getOpcode() == Instruction::Rem) |
| 1141 | Dest = makeAnotherReg(I.getType()); |
| 1142 | |
| 1143 | // FIXME: this is probably only right for 32 bit operands. |
| 1144 | if (I.getType ()->isSigned()) { |
| 1145 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 1146 | // Sign extend into the Y register |
| 1147 | BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31); |
| 1148 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0); |
| 1149 | BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
| 1150 | } else { |
| 1151 | // Zero extend into the Y register, ie, just set it to zero |
| 1152 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0); |
| 1153 | BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 1154 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1155 | |
| 1156 | if (I.getOpcode() == Instruction::Rem) { |
| 1157 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 1158 | BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg); |
| 1159 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp); |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 1160 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1161 | break; |
| 1162 | } |
| 1163 | default: |
| 1164 | visitInstruction (I); |
| 1165 | return; |
| 1166 | } |
| 1167 | |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1168 | static const unsigned Opcodes[] = { |
| 1169 | V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr, |
| 1170 | V8::SLLrr, V8::SRLrr, V8::SRArr |
| 1171 | }; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1172 | if (OpCase != ~0U) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1173 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Brian Gaeke | ccdd70a | 2004-07-08 08:08:10 +0000 | [diff] [blame] | 1176 | switch (getClassB (I.getType ())) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1177 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1178 | if (I.getType ()->isSigned ()) { // add byte |
| 1179 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 1180 | } else { // add ubyte |
| 1181 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 1182 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 1183 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 1184 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1185 | break; |
| 1186 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1187 | if (I.getType ()->isSigned ()) { // add short |
| 1188 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 1189 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 1190 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 1191 | } else { // add ushort |
| 1192 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 1193 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 1194 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1195 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1196 | break; |
| 1197 | case cInt: |
Brian Gaeke | ccdd70a | 2004-07-08 08:08:10 +0000 | [diff] [blame] | 1198 | // Nothing to do here. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1199 | break; |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1200 | case cLong: |
| 1201 | // Only support and, or, xor. |
| 1202 | if (OpCase < 3 || OpCase > 5) { |
| 1203 | visitInstruction (I); |
| 1204 | return; |
| 1205 | } |
| 1206 | // Do the other half of the value: |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1207 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1) |
| 1208 | .addReg (Op1Reg+1); |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1209 | break; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1210 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1211 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Misha Brukman | ea09126 | 2004-06-30 21:47:40 +0000 | [diff] [blame] | 1215 | void V8ISel::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1216 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 1217 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 1218 | unsigned DestReg = getReg (I); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1219 | const Type *Ty = I.getOperand (0)->getType (); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Compare the two values. |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1222 | assert (getClass (Ty) != cLong && "can't setcc on longs yet"); |
| 1223 | if (getClass (Ty) < cLong) { |
| 1224 | BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg); |
| 1225 | } else if (getClass (Ty) == cFloat) { |
| 1226 | BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg); |
| 1227 | } else if (getClass (Ty) == cDouble) { |
| 1228 | BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg); |
| 1229 | } |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1230 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1231 | unsigned BranchIdx; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1232 | switch (I.getOpcode()) { |
| 1233 | default: assert(0 && "Unknown setcc instruction!"); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1234 | case Instruction::SetEQ: BranchIdx = 0; break; |
| 1235 | case Instruction::SetNE: BranchIdx = 1; break; |
| 1236 | case Instruction::SetLT: BranchIdx = 2; break; |
| 1237 | case Instruction::SetGT: BranchIdx = 3; break; |
| 1238 | case Instruction::SetLE: BranchIdx = 4; break; |
| 1239 | case Instruction::SetGE: BranchIdx = 5; break; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1240 | } |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1241 | unsigned Column = 0; |
Brian Gaeke | b3e0017 | 2004-11-17 22:06:56 +0000 | [diff] [blame] | 1242 | if (Ty->isSigned() && !Ty->isFloatingPoint()) Column = 1; |
| 1243 | if (Ty->isFloatingPoint()) Column = 2; |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1244 | static unsigned OpcodeTab[3*6] = { |
| 1245 | // LLVM SparcV8 |
| 1246 | // unsigned signed fp |
| 1247 | V8::BE, V8::BE, V8::FBE, // seteq = be be fbe |
| 1248 | V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne |
| 1249 | V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl |
| 1250 | V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg |
| 1251 | V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble |
| 1252 | V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1253 | }; |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1254 | unsigned Opcode = OpcodeTab[3*BranchIdx + Column]; |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 1255 | |
| 1256 | MachineBasicBlock *thisMBB = BB; |
| 1257 | const BasicBlock *LLVM_BB = BB->getBasicBlock (); |
| 1258 | // thisMBB: |
| 1259 | // ... |
| 1260 | // subcc %reg0, %reg1, %g0 |
| 1261 | // bCC copy1MBB |
| 1262 | // ba copy0MBB |
| 1263 | |
| 1264 | // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB) |
| 1265 | // if we could insert other, non-terminator instructions after the |
| 1266 | // bCC. But MBB->getFirstTerminator() can't understand this. |
| 1267 | MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB); |
| 1268 | F->getBasicBlockList ().push_back (copy1MBB); |
| 1269 | BuildMI (BB, Opcode, 1).addMBB (copy1MBB); |
| 1270 | MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB); |
| 1271 | F->getBasicBlockList ().push_back (copy0MBB); |
| 1272 | BuildMI (BB, V8::BA, 1).addMBB (copy0MBB); |
| 1273 | // Update machine-CFG edges |
| 1274 | BB->addSuccessor (copy1MBB); |
| 1275 | BB->addSuccessor (copy0MBB); |
| 1276 | |
| 1277 | // copy0MBB: |
| 1278 | // %FalseValue = or %G0, 0 |
| 1279 | // ba sinkMBB |
| 1280 | BB = copy0MBB; |
| 1281 | unsigned FalseValue = makeAnotherReg (I.getType ()); |
| 1282 | BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0); |
| 1283 | MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB); |
| 1284 | F->getBasicBlockList ().push_back (sinkMBB); |
| 1285 | BuildMI (BB, V8::BA, 1).addMBB (sinkMBB); |
| 1286 | // Update machine-CFG edges |
| 1287 | BB->addSuccessor (sinkMBB); |
| 1288 | |
| 1289 | DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n"); |
| 1290 | DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n"); |
| 1291 | DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n"); |
| 1292 | DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n"); |
| 1293 | |
| 1294 | // copy1MBB: |
| 1295 | // %TrueValue = or %G0, 1 |
| 1296 | // ba sinkMBB |
| 1297 | BB = copy1MBB; |
| 1298 | unsigned TrueValue = makeAnotherReg (I.getType ()); |
| 1299 | BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1); |
| 1300 | BuildMI (BB, V8::BA, 1).addMBB (sinkMBB); |
| 1301 | // Update machine-CFG edges |
| 1302 | BB->addSuccessor (sinkMBB); |
| 1303 | |
| 1304 | // sinkMBB: |
| 1305 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ] |
| 1306 | // ... |
| 1307 | BB = sinkMBB; |
| 1308 | BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue) |
| 1309 | .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1312 | void V8ISel::visitAllocaInst(AllocaInst &I) { |
| 1313 | // Find the data size of the alloca inst's getAllocatedType. |
| 1314 | const Type *Ty = I.getAllocatedType(); |
| 1315 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1316 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1317 | unsigned ArraySizeReg = getReg (I.getArraySize ()); |
| 1318 | unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize)); |
| 1319 | unsigned TmpReg1 = makeAnotherReg (Type::UIntTy); |
| 1320 | unsigned TmpReg2 = makeAnotherReg (Type::UIntTy); |
| 1321 | unsigned StackAdjReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1322 | |
| 1323 | // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary |
| 1324 | BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 1325 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1326 | // Round up TmpReg1 to nearest doubleword boundary: |
| 1327 | BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7); |
| 1328 | BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 1329 | |
| 1330 | // Subtract size from stack pointer, thereby allocating some space. |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1331 | BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Put a pointer to the space into the result register, by copying |
| 1334 | // the stack pointer. |
| 1335 | BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96); |
| 1336 | |
| 1337 | // Inform the Frame Information that we have just allocated a variable-sized |
| 1338 | // object. |
| 1339 | F->getFrameInfo()->CreateVariableSizedObject(); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1340 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1341 | |
| 1342 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 1343 | /// function, lowering any calls to unknown intrinsic functions into the |
| 1344 | /// equivalent LLVM code. |
| 1345 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 1346 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1347 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 1348 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 1349 | if (Function *F = CI->getCalledFunction()) |
| 1350 | switch (F->getIntrinsicID()) { |
Brian Gaeke | d90282d | 2004-11-19 20:57:24 +0000 | [diff] [blame^] | 1351 | case Intrinsic::vastart: |
| 1352 | case Intrinsic::vacopy: |
| 1353 | case Intrinsic::vaend: |
| 1354 | // We directly implement these intrinsics |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1355 | case Intrinsic::not_intrinsic: break; |
| 1356 | default: |
| 1357 | // All other intrinsic calls we must lower. |
| 1358 | Instruction *Before = CI->getPrev(); |
| 1359 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 1360 | if (Before) { // Move iterator to instruction after call |
| 1361 | I = Before; ++I; |
| 1362 | } else { |
| 1363 | I = BB->begin(); |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1370 | switch (ID) { |
Brian Gaeke | 9e672a2 | 2004-11-19 18:53:59 +0000 | [diff] [blame] | 1371 | default: |
| 1372 | std::cerr << "Sorry, unknown intrinsic function call:\n" << CI; abort (); |
| 1373 | |
Brian Gaeke | d90282d | 2004-11-19 20:57:24 +0000 | [diff] [blame^] | 1374 | case Intrinsic::vastart: { |
| 1375 | unsigned DestReg = getReg (CI); |
| 1376 | BuildMI (BB, V8::ADDri, 2, DestReg).addReg (V8::FP).addSImm (VarArgsOffset); |
| 1377 | return; |
| 1378 | } |
Brian Gaeke | 9e672a2 | 2004-11-19 18:53:59 +0000 | [diff] [blame] | 1379 | |
| 1380 | case Intrinsic::vaend: |
Brian Gaeke | 2f95ed6 | 2004-11-19 19:21:34 +0000 | [diff] [blame] | 1381 | // va_end is a no-op on SparcV8. |
| 1382 | return; |
Brian Gaeke | 9e672a2 | 2004-11-19 18:53:59 +0000 | [diff] [blame] | 1383 | |
| 1384 | case Intrinsic::vacopy: |
| 1385 | std::cerr << "Sorry, va_copy intrinsic still unsupported:\n" << CI; abort (); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1386 | } |
| 1387 | } |