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