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