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" |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 9 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineFunctionInfo.h" |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 13 | #include "llvm/Function.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 17 | using std::vector; |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 18 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 19 | static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*) |
| 20 | static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR |
| 21 | |
| 22 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 23 | //---------------------------------------------------------------------------- |
| 24 | // Function: CreateSETUWConst |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 25 | // |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 26 | // Set a 32-bit unsigned constant in the register `dest', using |
| 27 | // SETHI, OR in the worst case. This function correctly emulates |
| 28 | // the SETUW pseudo-op for SPARC v9 (if argument isSigned == false). |
| 29 | // |
| 30 | // The isSigned=true case is used to implement SETSW without duplicating code. |
| 31 | // |
| 32 | // Optimize some common cases: |
| 33 | // (1) Small value that fits in simm13 field of OR: don't need SETHI. |
| 34 | // (2) isSigned = true and C is a small negative signed value, i.e., |
| 35 | // high bits are 1, and the remaining bits fit in simm13(OR). |
| 36 | //---------------------------------------------------------------------------- |
| 37 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 38 | static inline void |
| 39 | CreateSETUWConst(const TargetMachine& target, uint32_t C, |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 40 | Instruction* dest, vector<MachineInstr*>& mvec, |
| 41 | bool isSigned = false) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 42 | { |
| 43 | MachineInstr *miSETHI = NULL, *miOR = NULL; |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 44 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 45 | // In order to get efficient code, we should not generate the SETHI if |
| 46 | // all high bits are 1 (i.e., this is a small signed value that fits in |
| 47 | // the simm13 field of OR). So we check for and handle that case specially. |
| 48 | // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0. |
| 49 | // In fact, sC == -sC, so we have to check for this explicitly. |
| 50 | int32_t sC = (int32_t) C; |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 51 | bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM; |
| 52 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 53 | // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 54 | if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 55 | { |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 56 | miSETHI = BuildMI(SETHI, 2).addZImm(C).addRegDef(dest); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 57 | miSETHI->setOperandHi32(0); |
| 58 | mvec.push_back(miSETHI); |
| 59 | } |
| 60 | |
| 61 | // Set the low 10 or 12 bits in dest. This is necessary if no SETHI |
| 62 | // was generated, or if the low 10 bits are non-zero. |
| 63 | if (miSETHI==NULL || C & MAXLO) |
| 64 | { |
| 65 | if (miSETHI) |
| 66 | { // unsigned value with high-order bits set using SETHI |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 67 | miOR = BuildMI(OR, 3).addReg(dest).addZImm(C).addRegDef(dest); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 68 | miOR->setOperandLo32(1); |
| 69 | } |
| 70 | else |
| 71 | { // unsigned or small signed value that fits in simm13 field of OR |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 72 | assert(smallNegValue || (C & ~MAXSIMM) == 0); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 73 | miOR = new MachineInstr(OR); |
| 74 | miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum()); |
| 75 | miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, |
| 76 | sC); |
| 77 | miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest); |
| 78 | } |
| 79 | mvec.push_back(miOR); |
| 80 | } |
| 81 | |
| 82 | assert((miSETHI || miOR) && "Oops, no code was generated!"); |
| 83 | } |
| 84 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 85 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 86 | //---------------------------------------------------------------------------- |
| 87 | // Function: CreateSETSWConst |
| 88 | // |
| 89 | // Set a 32-bit signed constant in the register `dest', with sign-extension |
| 90 | // to 64 bits. This uses SETHI, OR, SRA in the worst case. |
| 91 | // This function correctly emulates the SETSW pseudo-op for SPARC v9. |
| 92 | // |
| 93 | // Optimize the same cases as SETUWConst, plus: |
| 94 | // (1) SRA is not needed for positive or small negative values. |
| 95 | //---------------------------------------------------------------------------- |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 96 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 97 | static inline void |
| 98 | CreateSETSWConst(const TargetMachine& target, int32_t C, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 99 | Instruction* dest, vector<MachineInstr*>& mvec) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 100 | { |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 101 | // Set the low 32 bits of dest |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 102 | CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true); |
| 103 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 104 | // Sign-extend to the high 32 bits if needed |
| 105 | if (C < 0 && (-C) > (int32_t) MAXSIMM) |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 106 | mvec.push_back(BuildMI(SRA, 3).addReg(dest).addZImm(0).addRegDef(dest)); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 110 | //---------------------------------------------------------------------------- |
| 111 | // Function: CreateSETXConst |
| 112 | // |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 113 | // Set a 64-bit signed or unsigned constant in the register `dest'. |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 114 | // Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between. |
| 115 | // This function correctly emulates the SETX pseudo-op for SPARC v9. |
| 116 | // |
| 117 | // Optimize the same cases as SETUWConst for each 32 bit word. |
| 118 | //---------------------------------------------------------------------------- |
| 119 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 120 | static inline void |
| 121 | CreateSETXConst(const TargetMachine& target, uint64_t C, |
| 122 | Instruction* tmpReg, Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 123 | vector<MachineInstr*>& mvec) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 124 | { |
| 125 | assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!"); |
| 126 | |
| 127 | MachineInstr* MI; |
| 128 | |
| 129 | // Code to set the upper 32 bits of the value in register `tmpReg' |
| 130 | CreateSETUWConst(target, (C >> 32), tmpReg, mvec); |
| 131 | |
| 132 | // Shift tmpReg left by 32 bits |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 133 | mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg)); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 134 | |
| 135 | // Code to set the low 32 bits of the value in register `dest' |
| 136 | CreateSETUWConst(target, C, dest, mvec); |
| 137 | |
| 138 | // dest = OR(tmpReg, dest) |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 139 | mvec.push_back(BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest)); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 143 | //---------------------------------------------------------------------------- |
| 144 | // Function: CreateSETUWLabel |
| 145 | // |
| 146 | // Set a 32-bit constant (given by a symbolic label) in the register `dest'. |
| 147 | //---------------------------------------------------------------------------- |
| 148 | |
| 149 | static inline void |
| 150 | CreateSETUWLabel(const TargetMachine& target, Value* val, |
| 151 | Instruction* dest, vector<MachineInstr*>& mvec) |
| 152 | { |
| 153 | MachineInstr* MI; |
| 154 | |
| 155 | // Set the high 22 bits in dest |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 156 | MI = BuildMI(SETHI, 2).addReg(val).addRegDef(dest); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 157 | MI->setOperandHi32(0); |
| 158 | mvec.push_back(MI); |
| 159 | |
| 160 | // Set the low 10 bits in dest |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 161 | MI = BuildMI(OR, 3).addReg(dest).addReg(val).addRegDef(dest); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 162 | MI->setOperandLo32(1); |
| 163 | mvec.push_back(MI); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | //---------------------------------------------------------------------------- |
| 168 | // Function: CreateSETXLabel |
| 169 | // |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 170 | // Set a 64-bit constant (given by a symbolic label) in the register `dest'. |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 171 | //---------------------------------------------------------------------------- |
| 172 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 173 | static inline void |
| 174 | CreateSETXLabel(const TargetMachine& target, |
| 175 | Value* val, Instruction* tmpReg, Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 176 | vector<MachineInstr*>& mvec) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 177 | { |
| 178 | assert(isa<Constant>(val) || isa<GlobalValue>(val) && |
| 179 | "I only know about constant values and global addresses"); |
| 180 | |
| 181 | MachineInstr* MI; |
| 182 | |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 183 | MI = BuildMI(SETHI, 2).addReg(val).addRegDef(tmpReg); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 184 | MI->setOperandHi64(0); |
| 185 | mvec.push_back(MI); |
| 186 | |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 187 | MI = BuildMI(OR, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 188 | MI->setOperandLo64(1); |
| 189 | mvec.push_back(MI); |
| 190 | |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 191 | mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg)); |
| 192 | MI = BuildMI(SETHI, 2).addPCDisp(val).addRegDef(dest); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 193 | MI->setOperandHi32(0); |
| 194 | mvec.push_back(MI); |
| 195 | |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 196 | MI = BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 197 | mvec.push_back(MI); |
| 198 | |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 199 | MI = BuildMI(OR, 3).addReg(dest).addPCDisp(val).addRegDef(dest); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 200 | MI->setOperandLo32(1); |
| 201 | mvec.push_back(MI); |
| 202 | } |
| 203 | |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 204 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 205 | //---------------------------------------------------------------------------- |
| 206 | // Function: CreateUIntSetInstruction |
| 207 | // |
| 208 | // Create code to Set an unsigned constant in the register `dest'. |
| 209 | // Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed. |
| 210 | // CreateSETSWConst is an optimization for the case that the unsigned value |
| 211 | // has all ones in the 33 high bits (so that sign-extension sets them all). |
| 212 | //---------------------------------------------------------------------------- |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 213 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 214 | static inline void |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 215 | CreateUIntSetInstruction(const TargetMachine& target, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 216 | uint64_t C, Instruction* dest, |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 217 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 218 | MachineCodeForInstruction& mcfi) |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 219 | { |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 220 | static const uint64_t lo32 = (uint32_t) ~0; |
| 221 | if (C <= lo32) // High 32 bits are 0. Set low 32 bits. |
| 222 | CreateSETUWConst(target, (uint32_t) C, dest, mvec); |
| 223 | else if ((C & ~lo32) == ~lo32 && (C & (1 << 31))) |
| 224 | { // All high 33 (not 32) bits are 1s: sign-extension will take care |
| 225 | // of high 32 bits, so use the sequence for signed int |
| 226 | CreateSETSWConst(target, (int32_t) C, dest, mvec); |
| 227 | } |
| 228 | else if (C > lo32) |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 229 | { // C does not fit in 32 bits |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 230 | TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 231 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 232 | CreateSETXConst(target, C, tmpReg, dest, mvec); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 233 | } |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 236 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 237 | //---------------------------------------------------------------------------- |
| 238 | // Function: CreateIntSetInstruction |
| 239 | // |
| 240 | // Create code to Set a signed constant in the register `dest'. |
| 241 | // Really the same as CreateUIntSetInstruction. |
| 242 | //---------------------------------------------------------------------------- |
| 243 | |
| 244 | static inline void |
| 245 | CreateIntSetInstruction(const TargetMachine& target, |
| 246 | int64_t C, Instruction* dest, |
| 247 | std::vector<MachineInstr*>& mvec, |
| 248 | MachineCodeForInstruction& mcfi) |
| 249 | { |
| 250 | CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi); |
| 251 | } |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 252 | |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 253 | |
| 254 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 255 | // Create a table of LLVM opcode -> max. immediate constant likely to |
| 256 | // be usable for that operation. |
| 257 | //--------------------------------------------------------------------------- |
| 258 | |
| 259 | // Entry == 0 ==> no immediate constant field exists at all. |
| 260 | // Entry > 0 ==> abs(immediate constant) <= Entry |
| 261 | // |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 262 | vector<int> MaxConstantsTable(Instruction::OtherOpsEnd); |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 263 | |
| 264 | static int |
| 265 | MaxConstantForInstr(unsigned llvmOpCode) |
| 266 | { |
| 267 | int modelOpCode = -1; |
| 268 | |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 269 | if (llvmOpCode >= Instruction::BinaryOpsBegin && |
| 270 | llvmOpCode < Instruction::BinaryOpsEnd) |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 271 | modelOpCode = ADD; |
| 272 | else |
| 273 | switch(llvmOpCode) { |
| 274 | case Instruction::Ret: modelOpCode = JMPLCALL; break; |
| 275 | |
| 276 | case Instruction::Malloc: |
| 277 | case Instruction::Alloca: |
| 278 | case Instruction::GetElementPtr: |
| 279 | case Instruction::PHINode: |
| 280 | case Instruction::Cast: |
| 281 | case Instruction::Call: modelOpCode = ADD; break; |
| 282 | |
| 283 | case Instruction::Shl: |
| 284 | case Instruction::Shr: modelOpCode = SLLX; break; |
| 285 | |
| 286 | default: break; |
| 287 | }; |
| 288 | |
| 289 | return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst; |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | InitializeMaxConstantsTable() |
| 294 | { |
| 295 | unsigned op; |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 296 | assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd && |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 297 | "assignments below will be illegal!"); |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 298 | for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op) |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 299 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 300 | for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op) |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 301 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 302 | for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op) |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 303 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 304 | for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op) |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 305 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 310 | // class UltraSparcInstrInfo |
| 311 | // |
| 312 | // Purpose: |
| 313 | // Information about individual instructions. |
| 314 | // Most information is stored in the SparcMachineInstrDesc array above. |
| 315 | // Other information is computed on demand, and most such functions |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 316 | // default to member functions in base class TargetInstrInfo. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 317 | //--------------------------------------------------------------------------- |
| 318 | |
| 319 | /*ctor*/ |
Chris Lattner | 047bbaf | 2002-10-29 15:45:20 +0000 | [diff] [blame] | 320 | UltraSparcInstrInfo::UltraSparcInstrInfo() |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 321 | : TargetInstrInfo(SparcMachineInstrDesc, |
| 322 | /*descSize = */ NUM_TOTAL_OPCODES, |
| 323 | /*numRealOpCodes = */ NUM_REAL_OPCODES) |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 324 | { |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 325 | InitializeMaxConstantsTable(); |
| 326 | } |
| 327 | |
| 328 | bool |
| 329 | UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV, |
| 330 | const Instruction* I) const |
| 331 | { |
| 332 | if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!) |
| 333 | return true; |
| 334 | |
| 335 | if (isa<ConstantPointerNull>(CV)) // can always use %g0 |
| 336 | return false; |
| 337 | |
| 338 | if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV)) |
Vikram S. Adve | 893cace | 2002-10-13 00:04:26 +0000 | [diff] [blame] | 339 | /* Large unsigned longs may really just be small negative signed longs */ |
| 340 | return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]); |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 341 | |
| 342 | if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV)) |
Vikram S. Adve | 893cace | 2002-10-13 00:04:26 +0000 | [diff] [blame] | 343 | return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]); |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 344 | |
| 345 | if (isa<ConstantBool>(CV)) |
Vikram S. Adve | 893cace | 2002-10-13 00:04:26 +0000 | [diff] [blame] | 346 | return (1 > MaxConstantsTable[I->getOpcode()]); |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 347 | |
| 348 | return true; |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 351 | // |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 352 | // Create an instruction sequence to put the constant `val' into |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 353 | // the virtual register `dest'. `val' may be a Constant or a |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 354 | // GlobalValue, viz., the constant address of a global variable or function. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 355 | // The generated instructions are returned in `mvec'. |
| 356 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 357 | // Any stack space required is allocated via MachineFunction. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 358 | // |
| 359 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 360 | UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target, |
| 361 | Function* F, |
| 362 | Value* val, |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 363 | Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 364 | vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 365 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 366 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 367 | assert(isa<Constant>(val) || isa<GlobalValue>(val) && |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 368 | "I only know about constant values and global addresses"); |
| 369 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 370 | // Use a "set" instruction for known constants or symbolic constants (labels) |
| 371 | // that can go in an integer reg. |
| 372 | // We have to use a "load" instruction for all other constants, |
| 373 | // in particular, floating point constants. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 374 | // |
| 375 | const Type* valType = val->getType(); |
| 376 | |
Vikram S. Adve | 893cace | 2002-10-13 00:04:26 +0000 | [diff] [blame] | 377 | // Unfortunate special case: a ConstantPointerRef is just a |
| 378 | // reference to GlobalValue. |
| 379 | if (isa<ConstantPointerRef>(val)) |
| 380 | val = cast<ConstantPointerRef>(val)->getValue(); |
| 381 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 382 | if (isa<GlobalValue>(val)) |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 383 | { |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 384 | TmpInstruction* tmpReg = |
| 385 | new TmpInstruction(PointerType::get(val->getType()), val); |
| 386 | mcfi.addTemp(tmpReg); |
| 387 | CreateSETXLabel(target, val, tmpReg, dest, mvec); |
| 388 | } |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 389 | else if (valType->isIntegral()) |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 390 | { |
| 391 | bool isValidConstant; |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 392 | unsigned opSize = target.getTargetData().getTypeSize(val->getType()); |
| 393 | unsigned destSize = target.getTargetData().getTypeSize(dest->getType()); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 394 | |
| 395 | if (! dest->getType()->isSigned()) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 396 | { |
Vikram S. Adve | a40cbb3 | 2002-08-04 20:55:37 +0000 | [diff] [blame] | 397 | uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant); |
| 398 | assert(isValidConstant && "Unrecognized constant"); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 399 | |
| 400 | if (opSize > destSize || |
| 401 | (val->getType()->isSigned() |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 402 | && destSize < target.getTargetData().getIntegerRegize())) |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 403 | { // operand is larger than dest, |
| 404 | // OR both are equal but smaller than the full register size |
| 405 | // AND operand is signed, so it may have extra sign bits: |
| 406 | // mask high bits |
| 407 | C = C & ((1U << 8*destSize) - 1); |
| 408 | } |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 409 | CreateUIntSetInstruction(target, C, dest, mvec, mcfi); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 410 | } |
| 411 | else |
| 412 | { |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 413 | int64_t C = GetConstantValueAsSignedInt(val, isValidConstant); |
| 414 | assert(isValidConstant && "Unrecognized constant"); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 415 | |
| 416 | if (opSize > destSize) |
| 417 | // operand is larger than dest: mask high bits |
| 418 | C = C & ((1U << 8*destSize) - 1); |
| 419 | |
| 420 | if (opSize > destSize || |
| 421 | (opSize == destSize && !val->getType()->isSigned())) |
| 422 | // sign-extend from destSize to 64 bits |
| 423 | C = ((C & (1U << (8*destSize - 1))) |
| 424 | ? C | ~((1U << 8*destSize) - 1) |
| 425 | : C); |
| 426 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 427 | CreateIntSetInstruction(target, C, dest, mvec, mcfi); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 428 | } |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 429 | } |
| 430 | else |
| 431 | { |
| 432 | // Make an instruction sequence to load the constant, viz: |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 433 | // SETX <addr-of-constant>, tmpReg, addrReg |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 434 | // LOAD /*addr*/ addrReg, /*offset*/ 0, dest |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 435 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 436 | // 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] | 437 | TmpInstruction* tmpReg = |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 438 | new TmpInstruction(PointerType::get(val->getType()), val); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 439 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 440 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 441 | // Create another TmpInstruction for the address register |
| 442 | TmpInstruction* addrReg = |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 443 | new TmpInstruction(PointerType::get(val->getType()), val); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 444 | mcfi.addTemp(addrReg); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 445 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 446 | // Put the address (a symbolic name) into a register |
| 447 | CreateSETXLabel(target, val, tmpReg, addrReg, mvec); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 448 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 449 | // Generate the load instruction |
| 450 | int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0 |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 451 | unsigned Opcode = ChooseLoadInstruction(val->getType()); |
| 452 | mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg). |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 453 | addSImm(zeroOffset).addRegDef(dest)); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 454 | |
| 455 | // Make sure constant is emitted to constant pool in assembly code. |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 456 | MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val)); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 461 | // Create an instruction sequence to copy an integer register `val' |
| 462 | // to a floating point register `dest' by copying to memory and back. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 463 | // 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] | 464 | // The generated instructions are returned in `mvec'. |
| 465 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 466 | // Any stack space required is allocated via MachineFunction. |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 467 | // |
| 468 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 469 | UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target, |
| 470 | Function* F, |
| 471 | Value* val, |
| 472 | Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 473 | vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 474 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 475 | { |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 476 | assert((val->getType()->isIntegral() || isa<PointerType>(val->getType())) |
| 477 | && "Source type must be integral (integer or bool) or pointer"); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 478 | assert(dest->getType()->isFloatingPoint() |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 479 | && "Dest type must be float/double"); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 480 | |
| 481 | // Get a stack slot to use for the copy |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 482 | int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 483 | |
| 484 | // Get the size of the source value being copied. |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 485 | size_t srcSize = target.getTargetData().getTypeSize(val->getType()); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 486 | |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 487 | // Store instruction stores `val' to [%fp+offset]. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 488 | // The store and load opCodes are based on the size of the source value. |
| 489 | // If the value is smaller than 32 bits, we must sign- or zero-extend it |
| 490 | // to 32 bits since the load-float will load 32 bits. |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 491 | // Note that the store instruction is the same for signed and unsigned ints. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 492 | const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy; |
| 493 | Value* storeVal = val; |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 494 | if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 495 | { // sign- or zero-extend respectively |
| 496 | storeVal = new TmpInstruction(storeType, val); |
| 497 | if (val->getType()->isSigned()) |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 498 | CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 499 | mvec, mcfi); |
| 500 | else |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 501 | CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 502 | mvec, mcfi); |
| 503 | } |
| 504 | MachineInstr* store=new MachineInstr(ChooseStoreInstruction(storeType)); |
| 505 | store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, storeVal); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 506 | store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 507 | store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset); |
| 508 | mvec.push_back(store); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 509 | |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 510 | // Load instruction loads [%fp+offset] to `dest'. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 511 | // The type of the load opCode is the floating point type that matches the |
| 512 | // stored type in size: |
| 513 | // On SparcV9: float for int or smaller, double for long. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 514 | // |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 515 | const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy; |
| 516 | MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadType)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 517 | load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
| 518 | load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset); |
| 519 | load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 520 | mvec.push_back(load); |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 523 | // Similarly, create an instruction sequence to copy an FP register |
| 524 | // `val' to an integer register `dest' by copying to memory and back. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 525 | // The generated instructions are returned in `mvec'. |
| 526 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 527 | // Any stack space required is allocated via MachineFunction. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 528 | // |
| 529 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 530 | UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target, |
| 531 | Function* F, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 532 | Value* val, |
| 533 | Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 534 | vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 535 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 536 | { |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 537 | const Type* opTy = val->getType(); |
| 538 | const Type* destTy = dest->getType(); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 539 | |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 540 | assert(opTy->isFloatingPoint() && "Source type must be float/double"); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 541 | assert((destTy->isIntegral() || isa<PointerType>(destTy)) |
| 542 | && "Dest type must be integer, bool or pointer"); |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 2ef9a6a | 2002-12-28 20:18:21 +0000 | [diff] [blame] | 544 | int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 545 | |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 546 | // Store instruction stores `val' to [%fp+offset]. |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 547 | // The store opCode is based only the source value being copied. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 548 | // |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 549 | MachineInstr* store=new MachineInstr(ChooseStoreInstruction(opTy)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 550 | store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val); |
| 551 | store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer()); |
| 552 | store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 553 | mvec.push_back(store); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 554 | |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 555 | // Load instruction loads [%fp+offset] to `dest'. |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 556 | // The type of the load opCode is the integer type that matches the |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 557 | // source type in size: |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 558 | // On SparcV9: int for float, long for double. |
| 559 | // Note that we *must* use signed loads even for unsigned dest types, to |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 560 | // ensure correct sign-extension for UByte, UShort or UInt: |
| 561 | // |
| 562 | const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy; |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 563 | MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 564 | load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 565 | load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 566 | load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 567 | mvec.push_back(load); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | // Create instruction(s) to copy src to dest, for arbitrary types |
| 572 | // The generated instructions are returned in `mvec'. |
| 573 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 574 | // Any stack space required is allocated via MachineFunction. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 575 | // |
| 576 | void |
| 577 | UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target, |
| 578 | Function *F, |
| 579 | Value* src, |
| 580 | Instruction* dest, |
| 581 | vector<MachineInstr*>& mvec, |
| 582 | MachineCodeForInstruction& mcfi) const |
| 583 | { |
| 584 | bool loadConstantToReg = false; |
| 585 | |
| 586 | const Type* resultType = dest->getType(); |
| 587 | |
| 588 | MachineOpCode opCode = ChooseAddInstructionByType(resultType); |
| 589 | if (opCode == INVALID_OPCODE) |
| 590 | { |
| 591 | assert(0 && "Unsupported result type in CreateCopyInstructionsByType()"); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | // if `src' is a constant that doesn't fit in the immed field or if it is |
| 596 | // a global variable (i.e., a constant address), generate a load |
| 597 | // instruction instead of an add |
| 598 | // |
| 599 | if (isa<Constant>(src)) |
| 600 | { |
| 601 | unsigned int machineRegNum; |
| 602 | int64_t immedValue; |
| 603 | MachineOperand::MachineOperandType opType = |
| 604 | ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true, |
| 605 | machineRegNum, immedValue); |
| 606 | |
| 607 | if (opType == MachineOperand::MO_VirtualRegister) |
| 608 | loadConstantToReg = true; |
| 609 | } |
| 610 | else if (isa<GlobalValue>(src)) |
| 611 | loadConstantToReg = true; |
| 612 | |
| 613 | if (loadConstantToReg) |
| 614 | { // `src' is constant and cannot fit in immed field for the ADD |
| 615 | // Insert instructions to "load" the constant into a register |
| 616 | target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest, |
| 617 | mvec, mcfi); |
| 618 | } |
| 619 | else |
| 620 | { // Create an add-with-0 instruction of the appropriate type. |
| 621 | // Make `src' the second operand, in case it is a constant |
| 622 | // Use (unsigned long) 0 for a NULL pointer value. |
| 623 | // |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 624 | const Type* Ty =isa<PointerType>(resultType) ? Type::ULongTy : resultType; |
| 625 | MachineInstr* MI = |
| 626 | BuildMI(opCode, 3).addReg(Constant::getNullValue(Ty)) |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 627 | .addReg(src).addRegDef(dest); |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 628 | mvec.push_back(MI); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 633 | // Helper function for sign-extension and zero-extension. |
| 634 | // For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL. |
| 635 | inline void |
| 636 | CreateBitExtensionInstructions(bool signExtend, |
| 637 | const TargetMachine& target, |
| 638 | Function* F, |
| 639 | Value* srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 640 | Value* destVal, |
| 641 | unsigned int numLowBits, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 642 | vector<MachineInstr*>& mvec, |
| 643 | MachineCodeForInstruction& mcfi) |
| 644 | { |
| 645 | MachineInstr* M; |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 646 | |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 647 | assert(numLowBits <= 32 && "Otherwise, nothing should be done here!"); |
| 648 | |
| 649 | if (numLowBits < 32) |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 650 | { // SLL is needed since operand size is < 32 bits. |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 651 | TmpInstruction *tmpI = new TmpInstruction(destVal->getType(), |
| 652 | srcVal, destVal, "make32"); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 653 | mcfi.addTemp(tmpI); |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 654 | mvec.push_back(BuildMI(SLLX, 3).addReg(srcVal).addZImm(32-numLowBits) |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 655 | .addRegDef(tmpI)); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 656 | srcVal = tmpI; |
| 657 | } |
| 658 | |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 659 | mvec.push_back(BuildMI(signExtend? SRA : SRL, 3).addReg(srcVal) |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame^] | 660 | .addZImm(32-numLowBits).addRegDef(destVal)); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 664 | // Create instruction sequence to produce a sign-extended register value |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 665 | // from an arbitrary-sized integer value (sized in bits, not bytes). |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 666 | // The generated instructions are returned in `mvec'. |
| 667 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 668 | // Any stack space required is allocated via MachineFunction. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 669 | // |
| 670 | void |
| 671 | UltraSparcInstrInfo::CreateSignExtensionInstructions( |
| 672 | const TargetMachine& target, |
| 673 | Function* F, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 674 | Value* srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 675 | Value* destVal, |
| 676 | unsigned int numLowBits, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 677 | vector<MachineInstr*>& mvec, |
| 678 | MachineCodeForInstruction& mcfi) const |
| 679 | { |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 680 | CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 681 | destVal, numLowBits, mvec, mcfi); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | |
| 685 | // Create instruction sequence to produce a zero-extended register value |
| 686 | // from an arbitrary-sized integer value (sized in bits, not bytes). |
| 687 | // For SPARC v9, we sign-extend the given operand using SLL; SRL. |
| 688 | // The generated instructions are returned in `mvec'. |
| 689 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 690 | // Any stack space required is allocated via MachineFunction. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 691 | // |
| 692 | void |
| 693 | UltraSparcInstrInfo::CreateZeroExtensionInstructions( |
| 694 | const TargetMachine& target, |
| 695 | Function* F, |
| 696 | Value* srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 697 | Value* destVal, |
| 698 | unsigned int numLowBits, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 699 | vector<MachineInstr*>& mvec, |
| 700 | MachineCodeForInstruction& mcfi) const |
| 701 | { |
| 702 | CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame] | 703 | destVal, numLowBits, mvec, mcfi); |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 704 | } |