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