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