Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 1 | //*************************************************************************** |
| 2 | // File: |
| 3 | // SparcInstrInfo.cpp |
| 4 | // |
| 5 | // Purpose: |
| 6 | // |
| 7 | // History: |
| 8 | // 10/15/01 - Vikram Adve - Created |
| 9 | //**************************************************************************/ |
| 10 | |
| 11 | |
| 12 | #include "SparcInternals.h" |
| 13 | #include "SparcInstrSelectionSupport.h" |
| 14 | #include "llvm/Target/Sparc.h" |
| 15 | #include "llvm/CodeGen/InstrSelection.h" |
| 16 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 20 | #include "llvm/BasicBlock.h" |
| 21 | #include "llvm/Instruction.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 24 | using std::vector; |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 25 | |
| 26 | //************************ Internal Functions ******************************/ |
| 27 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 28 | static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*) |
| 29 | static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR |
| 30 | |
| 31 | |
| 32 | // Set a 32-bit unsigned constant in the register `dest'. |
| 33 | // |
| 34 | static inline void |
| 35 | CreateSETUWConst(const TargetMachine& target, uint32_t C, |
| 36 | Instruction* dest, std::vector<MachineInstr*>& mvec) |
| 37 | { |
| 38 | MachineInstr *miSETHI = NULL, *miOR = NULL; |
| 39 | |
| 40 | // In order to get efficient code, we should not generate the SETHI if |
| 41 | // all high bits are 1 (i.e., this is a small signed value that fits in |
| 42 | // the simm13 field of OR). So we check for and handle that case specially. |
| 43 | // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0. |
| 44 | // In fact, sC == -sC, so we have to check for this explicitly. |
| 45 | int32_t sC = (int32_t) C; |
| 46 | bool smallSignedValue = sC < 0 && sC != -sC && -sC < (int32_t) MAXSIMM; |
| 47 | |
| 48 | // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough |
| 49 | if (!smallSignedValue && (C & ~MAXLO) && C > MAXSIMM) |
| 50 | { |
| 51 | miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest); |
| 52 | miSETHI->setOperandHi32(0); |
| 53 | mvec.push_back(miSETHI); |
| 54 | } |
| 55 | |
| 56 | // Set the low 10 or 12 bits in dest. This is necessary if no SETHI |
| 57 | // was generated, or if the low 10 bits are non-zero. |
| 58 | if (miSETHI==NULL || C & MAXLO) |
| 59 | { |
| 60 | if (miSETHI) |
| 61 | { // unsigned value with high-order bits set using SETHI |
| 62 | miOR = Create3OperandInstr_UImmed(OR, dest, C, dest); |
| 63 | miOR->setOperandLo32(1); |
| 64 | } |
| 65 | else |
| 66 | { // unsigned or small signed value that fits in simm13 field of OR |
| 67 | assert(smallSignedValue || (C & ~MAXSIMM) == 0); |
| 68 | miOR = new MachineInstr(OR); |
| 69 | miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum()); |
| 70 | miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, |
| 71 | sC); |
| 72 | miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest); |
| 73 | } |
| 74 | mvec.push_back(miOR); |
| 75 | } |
| 76 | |
| 77 | assert((miSETHI || miOR) && "Oops, no code was generated!"); |
| 78 | } |
| 79 | |
| 80 | // Set a 32-bit constant (given by a symbolic label) in the register `dest'. |
| 81 | // Not needed for SPARC v9 but useful to make the two SETX functions similar |
| 82 | static inline void |
| 83 | CreateSETUWLabel(const TargetMachine& target, Value* val, |
| 84 | Instruction* dest, std::vector<MachineInstr*>& mvec) |
| 85 | { |
| 86 | MachineInstr* MI; |
| 87 | |
| 88 | // Set the high 22 bits in dest |
| 89 | MI = Create2OperandInstr(SETHI, val, dest); |
| 90 | MI->setOperandHi32(0); |
| 91 | mvec.push_back(MI); |
| 92 | |
| 93 | // Set the low 10 bits in dest |
| 94 | MI = Create3OperandInstr(OR, dest, val, dest); |
| 95 | MI->setOperandLo32(1); |
| 96 | mvec.push_back(MI); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | // Set a 32-bit signed constant in the register `dest', |
| 101 | // with sign-extension to 64 bits. |
| 102 | static inline void |
| 103 | CreateSETSWConst(const TargetMachine& target, int32_t C, |
| 104 | Instruction* dest, std::vector<MachineInstr*>& mvec) |
| 105 | { |
| 106 | MachineInstr* MI; |
| 107 | |
| 108 | // Set the low 32 bits of dest |
| 109 | CreateSETUWConst(target, (uint32_t) C, dest, mvec); |
| 110 | |
| 111 | // Sign-extend to the high 32 bits if needed |
| 112 | if (C < 0 && (-C) > (int32_t) MAXSIMM) |
| 113 | { |
| 114 | MI = Create3OperandInstr_UImmed(SRA, dest, 0, dest); |
| 115 | mvec.push_back(MI); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // Set a 64-bit signed or unsigned constant in the register `dest'. |
| 121 | static inline void |
| 122 | CreateSETXConst(const TargetMachine& target, uint64_t C, |
| 123 | Instruction* tmpReg, Instruction* dest, |
| 124 | std::vector<MachineInstr*>& mvec) |
| 125 | { |
| 126 | assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!"); |
| 127 | |
| 128 | MachineInstr* MI; |
| 129 | |
| 130 | // Code to set the upper 32 bits of the value in register `tmpReg' |
| 131 | CreateSETUWConst(target, (C >> 32), tmpReg, mvec); |
| 132 | |
| 133 | // Shift tmpReg left by 32 bits |
| 134 | MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg); |
| 135 | mvec.push_back(MI); |
| 136 | |
| 137 | // Code to set the low 32 bits of the value in register `dest' |
| 138 | CreateSETUWConst(target, C, dest, mvec); |
| 139 | |
| 140 | // dest = OR(tmpReg, dest) |
| 141 | MI = Create3OperandInstr(OR, dest, tmpReg, dest); |
| 142 | mvec.push_back(MI); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | // Set a 64-bit constant (given by a symbolic label) in the register `dest'. |
| 147 | static inline void |
| 148 | CreateSETXLabel(const TargetMachine& target, |
| 149 | Value* val, Instruction* tmpReg, Instruction* dest, |
| 150 | std::vector<MachineInstr*>& mvec) |
| 151 | { |
| 152 | assert(isa<Constant>(val) || isa<GlobalValue>(val) && |
| 153 | "I only know about constant values and global addresses"); |
| 154 | |
| 155 | MachineInstr* MI; |
| 156 | |
| 157 | MI = Create2OperandInstr_Addr(SETHI, val, tmpReg); |
| 158 | MI->setOperandHi64(0); |
| 159 | mvec.push_back(MI); |
| 160 | |
| 161 | MI = Create3OperandInstr_Addr(OR, tmpReg, val, tmpReg); |
| 162 | MI->setOperandLo64(1); |
| 163 | mvec.push_back(MI); |
| 164 | |
| 165 | MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg); |
| 166 | mvec.push_back(MI); |
| 167 | |
| 168 | MI = Create2OperandInstr_Addr(SETHI, val, dest); |
| 169 | MI->setOperandHi32(0); |
| 170 | mvec.push_back(MI); |
| 171 | |
| 172 | MI = Create3OperandInstr(OR, dest, tmpReg, dest); |
| 173 | mvec.push_back(MI); |
| 174 | |
| 175 | MI = Create3OperandInstr_Addr(OR, dest, val, dest); |
| 176 | MI->setOperandLo32(1); |
| 177 | mvec.push_back(MI); |
| 178 | } |
| 179 | |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 180 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 181 | static inline void |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 182 | CreateIntSetInstruction(const TargetMachine& target, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 183 | int64_t C, Instruction* dest, |
| 184 | std::vector<MachineInstr*>& mvec, |
| 185 | MachineCodeForInstruction& mcfi) |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 186 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 187 | assert(dest->getType()->isSigned() && "Use CreateUIntSetInstruction()"); |
| 188 | |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 189 | uint64_t absC = (C >= 0)? C : -C; |
| 190 | if (absC > (unsigned int) ~0) |
| 191 | { // C does not fit in 32 bits |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 192 | TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 193 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 194 | CreateSETXConst(target, (uint64_t) C, tmpReg, dest, mvec); |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 195 | } |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 196 | else |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 197 | CreateSETSWConst(target, (int32_t) C, dest, mvec); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 200 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 201 | static inline void |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 202 | CreateUIntSetInstruction(const TargetMachine& target, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 203 | uint64_t C, Instruction* dest, |
| 204 | std::vector<MachineInstr*>& mvec, |
| 205 | MachineCodeForInstruction& mcfi) |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 206 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 207 | assert(! dest->getType()->isSigned() && "Use CreateIntSetInstruction()"); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 208 | MachineInstr* M; |
| 209 | |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 210 | if (C > (unsigned int) ~0) |
| 211 | { // C does not fit in 32 bits |
Vikram S. Adve | f7cedec | 2002-03-31 00:13:12 +0000 | [diff] [blame] | 212 | assert(dest->getType() == Type::ULongTy && "Sign extension problems"); |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 213 | TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 214 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 215 | CreateSETXConst(target, C, tmpReg, dest, mvec); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 216 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 217 | else |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 218 | { |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 219 | #undef SIGN_EXTEND_FOR_UNSIGNED_DEST |
| 220 | #ifdef SIGN_EXTEND_FOR_UNSIGNED_DEST |
| 221 | // If dest is smaller than the standard integer reg. size |
| 222 | // and the high-order bit of dest will be 1, then we have to |
| 223 | // extend the sign-bit into upper bits of the dest register. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 224 | // |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 225 | unsigned destSize = target.DataLayout.getTypeSize(dest->getType()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 226 | if (destSize < target.DataLayout.getIntegerRegize()) |
| 227 | { |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 228 | assert(destSize <= 4 && "Unexpected type size of 5-7 bytes"); |
| 229 | uint32_t signBit = C & (1 << (8*destSize-1)); |
| 230 | if (signBit) |
| 231 | { // Sign-bit is 1 so convert C to a sign-extended 64-bit value |
| 232 | // and use CreateSETSWConst. CreateSETSWConst will correctly |
| 233 | // generate efficient code for small signed values. |
| 234 | int32_t simmC = C | ~(signBit-1); |
| 235 | CreateSETSWConst(target, simmC, dest, mvec); |
| 236 | return; |
| 237 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 238 | } |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 239 | #endif SIGN_EXTEND_FOR_UNSIGNED_DEST |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 240 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 241 | CreateSETUWConst(target, C, dest, mvec); |
Vikram S. Adve | f7cedec | 2002-03-31 00:13:12 +0000 | [diff] [blame] | 242 | } |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 245 | |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 246 | //************************* External Classes *******************************/ |
| 247 | |
| 248 | //--------------------------------------------------------------------------- |
| 249 | // class UltraSparcInstrInfo |
| 250 | // |
| 251 | // Purpose: |
| 252 | // Information about individual instructions. |
| 253 | // Most information is stored in the SparcMachineInstrDesc array above. |
| 254 | // Other information is computed on demand, and most such functions |
| 255 | // default to member functions in base class MachineInstrInfo. |
| 256 | //--------------------------------------------------------------------------- |
| 257 | |
| 258 | /*ctor*/ |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 259 | UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt) |
| 260 | : MachineInstrInfo(tgt, SparcMachineInstrDesc, |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 261 | /*descSize = */ NUM_TOTAL_OPCODES, |
| 262 | /*numRealOpCodes = */ NUM_REAL_OPCODES) |
| 263 | { |
| 264 | } |
| 265 | |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 266 | // |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 267 | // Create an instruction sequence to put the constant `val' into |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 268 | // the virtual register `dest'. `val' may be a Constant or a |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 269 | // GlobalValue, viz., the constant address of a global variable or function. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 270 | // The generated instructions are returned in `mvec'. |
| 271 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 272 | // Any stack space required is allocated via MachineCodeForMethod. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 273 | // |
| 274 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 275 | UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target, |
| 276 | Function* F, |
| 277 | Value* val, |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 278 | Instruction* dest, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 279 | std::vector<MachineInstr*>& mvec, |
| 280 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 281 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 282 | assert(isa<Constant>(val) || isa<GlobalValue>(val) && |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 283 | "I only know about constant values and global addresses"); |
| 284 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 285 | // Use a "set" instruction for known constants or symbolic constants (labels) |
| 286 | // that can go in an integer reg. |
| 287 | // We have to use a "load" instruction for all other constants, |
| 288 | // in particular, floating point constants. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 289 | // |
| 290 | const Type* valType = val->getType(); |
| 291 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 292 | if (isa<GlobalValue>(val) || valType->isIntegral() || valType == Type::BoolTy) |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 293 | { |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 294 | if (isa<GlobalValue>(val)) |
| 295 | { |
| 296 | TmpInstruction* tmpReg = |
| 297 | new TmpInstruction(PointerType::get(val->getType()), val); |
| 298 | mcfi.addTemp(tmpReg); |
| 299 | CreateSETXLabel(target, val, tmpReg, dest, mvec); |
| 300 | } |
| 301 | else if (! val->getType()->isSigned()) |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 302 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 303 | uint64_t C = cast<ConstantUInt>(val)->getValue(); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 304 | CreateUIntSetInstruction(target, C, dest, mvec, mcfi); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 305 | } |
| 306 | else |
| 307 | { |
| 308 | bool isValidConstant; |
| 309 | int64_t C = GetConstantValueAsSignedInt(val, isValidConstant); |
| 310 | assert(isValidConstant && "Unrecognized constant"); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 311 | CreateIntSetInstruction(target, C, dest, mvec, mcfi); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 312 | } |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 313 | } |
| 314 | else |
| 315 | { |
| 316 | // Make an instruction sequence to load the constant, viz: |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 317 | // SETX <addr-of-constant>, tmpReg, addrReg |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 318 | // LOAD /*addr*/ addrReg, /*offset*/ 0, dest |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 319 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 320 | // First, create a tmp register to be used by the SETX sequence. |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 321 | TmpInstruction* tmpReg = |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 322 | new TmpInstruction(PointerType::get(val->getType()), val); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 323 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 324 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 325 | // Create another TmpInstruction for the address register |
| 326 | TmpInstruction* addrReg = |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 327 | new TmpInstruction(PointerType::get(val->getType()), val); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 328 | mcfi.addTemp(addrReg); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 329 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 330 | // Put the address (a symbolic name) into a register |
| 331 | CreateSETXLabel(target, val, tmpReg, addrReg, mvec); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 332 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 333 | // Generate the load instruction |
| 334 | int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0 |
| 335 | MachineInstr* MI = |
| 336 | Create3OperandInstr_SImmed(ChooseLoadInstruction(val->getType()), |
| 337 | addrReg, zeroOffset, dest); |
| 338 | mvec.push_back(MI); |
| 339 | |
| 340 | // Make sure constant is emitted to constant pool in assembly code. |
| 341 | MachineCodeForMethod::get(F).addToConstantPool(cast<Constant>(val)); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 346 | // Create an instruction sequence to copy an integer value `val' |
| 347 | // to a floating point value `dest' by copying to memory and back. |
| 348 | // val must be an integral type. dest must be a Float or Double. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 349 | // The generated instructions are returned in `mvec'. |
| 350 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 351 | // Any stack space required is allocated via MachineCodeForMethod. |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 352 | // |
| 353 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 354 | UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target, |
| 355 | Function* F, |
| 356 | Value* val, |
| 357 | Instruction* dest, |
| 358 | std::vector<MachineInstr*>& mvec, |
| 359 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 360 | { |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 361 | assert((val->getType()->isIntegral() || isa<PointerType>(val->getType())) |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 362 | && "Source type must be integral"); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 363 | assert(dest->getType()->isFloatingPoint() |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 364 | && "Dest type must be float/double"); |
| 365 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 366 | int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val); |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 367 | |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 368 | // Store instruction stores `val' to [%fp+offset]. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 369 | // The store and load opCodes are based on the value being copied, and |
Vikram S. Adve | b9959d8 | 2001-11-15 14:59:56 +0000 | [diff] [blame] | 370 | // they use integer and float types that accomodate the |
| 371 | // larger of the source type and the destination type: |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 372 | // On SparcV9: int for float, long for double. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 373 | // |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 374 | Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy |
| 375 | : Type::LongTy; |
| 376 | MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 377 | store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val); |
| 378 | store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 379 | store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset); |
| 380 | mvec.push_back(store); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 381 | |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 382 | // Load instruction loads [%fp+offset] to `dest'. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 383 | // |
Vikram S. Adve | b9959d8 | 2001-11-15 14:59:56 +0000 | [diff] [blame] | 384 | MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType())); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 385 | load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
| 386 | load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset); |
| 387 | load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 388 | mvec.push_back(load); |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
| 392 | // Similarly, create an instruction sequence to copy an FP value |
| 393 | // `val' to an integer value `dest' by copying to memory and back. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 394 | // The generated instructions are returned in `mvec'. |
| 395 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 396 | // Any stack space required is allocated via MachineCodeForMethod. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 397 | // |
| 398 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 399 | UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target, |
| 400 | Function* F, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 401 | Value* val, |
| 402 | Instruction* dest, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 403 | std::vector<MachineInstr*>& mvec, |
| 404 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 405 | { |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 406 | assert(val->getType()->isFloatingPoint() |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 407 | && "Source type must be float/double"); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 408 | assert((dest->getType()->isIntegral() || isa<PointerType>(dest->getType())) |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 409 | && "Dest type must be integral"); |
| 410 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 411 | int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val); |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 412 | |
| 413 | // Store instruction stores `val' to [%fp+offset]. |
| 414 | // The store and load opCodes are based on the value being copied, and |
| 415 | // they use the integer type that matches the source type in size: |
| 416 | // On SparcV9: int for float, long for double. |
| 417 | // |
| 418 | Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy |
| 419 | : Type::LongTy; |
Vikram S. Adve | b9959d8 | 2001-11-15 14:59:56 +0000 | [diff] [blame] | 420 | MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType())); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 421 | store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val); |
| 422 | store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer()); |
| 423 | store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 424 | mvec.push_back(store); |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 425 | |
| 426 | // Load instruction loads [%fp+offset] to `dest'. |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 427 | // |
| 428 | MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 429 | load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 430 | load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 431 | load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 432 | mvec.push_back(load); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | // Create instruction(s) to copy src to dest, for arbitrary types |
| 437 | // The generated instructions are returned in `mvec'. |
| 438 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 439 | // Any stack space required is allocated via MachineCodeForMethod. |
| 440 | // |
| 441 | void |
| 442 | UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target, |
| 443 | Function *F, |
| 444 | Value* src, |
| 445 | Instruction* dest, |
| 446 | vector<MachineInstr*>& mvec, |
| 447 | MachineCodeForInstruction& mcfi) const |
| 448 | { |
| 449 | bool loadConstantToReg = false; |
| 450 | |
| 451 | const Type* resultType = dest->getType(); |
| 452 | |
| 453 | MachineOpCode opCode = ChooseAddInstructionByType(resultType); |
| 454 | if (opCode == INVALID_OPCODE) |
| 455 | { |
| 456 | assert(0 && "Unsupported result type in CreateCopyInstructionsByType()"); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | // if `src' is a constant that doesn't fit in the immed field or if it is |
| 461 | // a global variable (i.e., a constant address), generate a load |
| 462 | // instruction instead of an add |
| 463 | // |
| 464 | if (isa<Constant>(src)) |
| 465 | { |
| 466 | unsigned int machineRegNum; |
| 467 | int64_t immedValue; |
| 468 | MachineOperand::MachineOperandType opType = |
| 469 | ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true, |
| 470 | machineRegNum, immedValue); |
| 471 | |
| 472 | if (opType == MachineOperand::MO_VirtualRegister) |
| 473 | loadConstantToReg = true; |
| 474 | } |
| 475 | else if (isa<GlobalValue>(src)) |
| 476 | loadConstantToReg = true; |
| 477 | |
| 478 | if (loadConstantToReg) |
| 479 | { // `src' is constant and cannot fit in immed field for the ADD |
| 480 | // Insert instructions to "load" the constant into a register |
| 481 | target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest, |
| 482 | mvec, mcfi); |
| 483 | } |
| 484 | else |
| 485 | { // Create an add-with-0 instruction of the appropriate type. |
| 486 | // Make `src' the second operand, in case it is a constant |
| 487 | // Use (unsigned long) 0 for a NULL pointer value. |
| 488 | // |
| 489 | const Type* zeroValueType = |
| 490 | isa<PointerType>(resultType) ? Type::ULongTy : resultType; |
| 491 | MachineInstr* minstr = |
| 492 | Create3OperandInstr(opCode, Constant::getNullValue(zeroValueType), |
| 493 | src, dest); |
| 494 | mvec.push_back(minstr); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | |
| 499 | // Create instruction sequence to produce a sign-extended register value |
| 500 | // from an arbitrary sized value (sized in bits, not bytes). |
| 501 | // For SPARC v9, we sign-extend the given unsigned operand using SLL; SRA. |
| 502 | // The generated instructions are returned in `mvec'. |
| 503 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 504 | // Any stack space required is allocated via MachineCodeForMethod. |
| 505 | // |
| 506 | void |
| 507 | UltraSparcInstrInfo::CreateSignExtensionInstructions( |
| 508 | const TargetMachine& target, |
| 509 | Function* F, |
| 510 | Value* unsignedSrcVal, |
| 511 | unsigned int srcSizeInBits, |
| 512 | Value* dest, |
| 513 | vector<MachineInstr*>& mvec, |
| 514 | MachineCodeForInstruction& mcfi) const |
| 515 | { |
| 516 | MachineInstr* M; |
| 517 | |
| 518 | assert(srcSizeInBits > 0 && srcSizeInBits <= 32 |
| 519 | && "Hmmm... srcSizeInBits > 32 unexpected but could be handled here."); |
| 520 | |
| 521 | if (srcSizeInBits < 32) |
| 522 | { // SLL is needed since operand size is < 32 bits. |
| 523 | TmpInstruction *tmpI = new TmpInstruction(dest->getType(), |
| 524 | unsignedSrcVal, dest,"make32"); |
| 525 | mcfi.addTemp(tmpI); |
| 526 | M = Create3OperandInstr_UImmed(SLL,unsignedSrcVal,32-srcSizeInBits,tmpI); |
| 527 | mvec.push_back(M); |
| 528 | unsignedSrcVal = tmpI; |
| 529 | } |
| 530 | |
| 531 | M = Create3OperandInstr_UImmed(SRA, unsignedSrcVal, 32-srcSizeInBits, dest); |
| 532 | mvec.push_back(M); |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 533 | } |