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