Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 1 | // $Id$ |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // SparcInstrSelection.cpp |
| 5 | // |
| 6 | // Purpose: |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 7 | // BURS instruction selection for SPARC V9 architecture. |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 8 | // |
| 9 | // History: |
| 10 | // 7/02/01 - Vikram Adve - Created |
| 11 | //**************************************************************************/ |
| 12 | |
| 13 | #include "SparcInternals.h" |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 14 | #include "SparcInstrSelectionSupport.h" |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 15 | #include "SparcRegClassInfo.h" |
Vikram S. Adve | 8557b22 | 2001-10-10 20:56:33 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
| 18 | #include "llvm/CodeGen/InstrForest.h" |
| 19 | #include "llvm/CodeGen/InstrSelection.h" |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
| 21 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/iTerminators.h" |
| 24 | #include "llvm/iMemory.h" |
| 25 | #include "llvm/iOther.h" |
| 26 | #include "llvm/BasicBlock.h" |
| 27 | #include "llvm/Method.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 28 | #include "llvm/ConstantVals.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 29 | #include "Support/MathExtras.h" |
Chris Lattner | 749655f | 2001-10-13 06:54:30 +0000 | [diff] [blame] | 30 | #include <math.h> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 31 | using std::vector; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 32 | |
| 33 | //************************* Forward Declarations ***************************/ |
| 34 | |
| 35 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 36 | static void SetMemOperands_Internal (vector<MachineInstr*>& mvec, |
| 37 | vector<MachineInstr*>::iterator mvecI, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 38 | const InstructionNode* vmInstrNode, |
| 39 | Value* ptrVal, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 40 | std::vector<Value*>& idxVec, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 41 | const TargetMachine& target); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | //************************ Internal Functions ******************************/ |
| 45 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 47 | static inline MachineOpCode |
| 48 | ChooseBprInstruction(const InstructionNode* instrNode) |
| 49 | { |
| 50 | MachineOpCode opCode; |
| 51 | |
| 52 | Instruction* setCCInstr = |
| 53 | ((InstructionNode*) instrNode->leftChild())->getInstruction(); |
| 54 | |
| 55 | switch(setCCInstr->getOpcode()) |
| 56 | { |
| 57 | case Instruction::SetEQ: opCode = BRZ; break; |
| 58 | case Instruction::SetNE: opCode = BRNZ; break; |
| 59 | case Instruction::SetLE: opCode = BRLEZ; break; |
| 60 | case Instruction::SetGE: opCode = BRGEZ; break; |
| 61 | case Instruction::SetLT: opCode = BRLZ; break; |
| 62 | case Instruction::SetGT: opCode = BRGZ; break; |
| 63 | default: |
| 64 | assert(0 && "Unrecognized VM instruction!"); |
| 65 | opCode = INVALID_OPCODE; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | return opCode; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static inline MachineOpCode |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 74 | ChooseBpccInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 75 | const BinaryOperator* setCCInstr) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 76 | { |
| 77 | MachineOpCode opCode = INVALID_OPCODE; |
| 78 | |
| 79 | bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned(); |
| 80 | |
| 81 | if (isSigned) |
| 82 | { |
| 83 | switch(setCCInstr->getOpcode()) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 84 | { |
| 85 | case Instruction::SetEQ: opCode = BE; break; |
| 86 | case Instruction::SetNE: opCode = BNE; break; |
| 87 | case Instruction::SetLE: opCode = BLE; break; |
| 88 | case Instruction::SetGE: opCode = BGE; break; |
| 89 | case Instruction::SetLT: opCode = BL; break; |
| 90 | case Instruction::SetGT: opCode = BG; break; |
| 91 | default: |
| 92 | assert(0 && "Unrecognized VM instruction!"); |
| 93 | break; |
| 94 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 95 | } |
| 96 | else |
| 97 | { |
| 98 | switch(setCCInstr->getOpcode()) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 99 | { |
| 100 | case Instruction::SetEQ: opCode = BE; break; |
| 101 | case Instruction::SetNE: opCode = BNE; break; |
| 102 | case Instruction::SetLE: opCode = BLEU; break; |
| 103 | case Instruction::SetGE: opCode = BCC; break; |
| 104 | case Instruction::SetLT: opCode = BCS; break; |
| 105 | case Instruction::SetGT: opCode = BGU; break; |
| 106 | default: |
| 107 | assert(0 && "Unrecognized VM instruction!"); |
| 108 | break; |
| 109 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return opCode; |
| 113 | } |
| 114 | |
| 115 | static inline MachineOpCode |
| 116 | ChooseBFpccInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 117 | const BinaryOperator* setCCInstr) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 118 | { |
| 119 | MachineOpCode opCode = INVALID_OPCODE; |
| 120 | |
| 121 | switch(setCCInstr->getOpcode()) |
| 122 | { |
| 123 | case Instruction::SetEQ: opCode = FBE; break; |
| 124 | case Instruction::SetNE: opCode = FBNE; break; |
| 125 | case Instruction::SetLE: opCode = FBLE; break; |
| 126 | case Instruction::SetGE: opCode = FBGE; break; |
| 127 | case Instruction::SetLT: opCode = FBL; break; |
| 128 | case Instruction::SetGT: opCode = FBG; break; |
| 129 | default: |
| 130 | assert(0 && "Unrecognized VM instruction!"); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | return opCode; |
| 135 | } |
| 136 | |
| 137 | |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 138 | // Create a unique TmpInstruction for a boolean value, |
| 139 | // representing the CC register used by a branch on that value. |
| 140 | // For now, hack this using a little static cache of TmpInstructions. |
| 141 | // Eventually the entire BURG instruction selection should be put |
| 142 | // into a separate class that can hold such information. |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 143 | // The static cache is not too bad because the memory for these |
| 144 | // TmpInstructions will be freed along with the rest of the Method anyway. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 145 | // |
| 146 | static TmpInstruction* |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 147 | GetTmpForCC(Value* boolVal, const Method* method, const Type* ccType) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 148 | { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 149 | typedef std::hash_map<const Value*, TmpInstruction*> BoolTmpCache; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 150 | static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction* |
| 151 | static const Method* lastMethod = NULL; // Use to flush cache between methods |
| 152 | |
| 153 | assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert"); |
| 154 | |
| 155 | if (lastMethod != method) |
| 156 | { |
| 157 | lastMethod = method; |
| 158 | boolToTmpCache.clear(); |
| 159 | } |
| 160 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 161 | // Look for tmpI and create a new one otherwise. The new value is |
| 162 | // directly written to map using the ref returned by operator[]. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 163 | TmpInstruction*& tmpI = boolToTmpCache[boolVal]; |
| 164 | if (tmpI == NULL) |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 165 | tmpI = new TmpInstruction(ccType, boolVal); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 166 | |
| 167 | return tmpI; |
| 168 | } |
| 169 | |
| 170 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 171 | static inline MachineOpCode |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 172 | ChooseBccInstruction(const InstructionNode* instrNode, |
| 173 | bool& isFPBranch) |
| 174 | { |
| 175 | InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild(); |
| 176 | BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction(); |
| 177 | const Type* setCCType = setCCInstr->getOperand(0)->getType(); |
| 178 | |
| 179 | isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy); |
| 180 | |
| 181 | if (isFPBranch) |
| 182 | return ChooseBFpccInstruction(instrNode, setCCInstr); |
| 183 | else |
| 184 | return ChooseBpccInstruction(instrNode, setCCInstr); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | static inline MachineOpCode |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 189 | ChooseMovFpccInstruction(const InstructionNode* instrNode) |
| 190 | { |
| 191 | MachineOpCode opCode = INVALID_OPCODE; |
| 192 | |
| 193 | switch(instrNode->getInstruction()->getOpcode()) |
| 194 | { |
| 195 | case Instruction::SetEQ: opCode = MOVFE; break; |
| 196 | case Instruction::SetNE: opCode = MOVFNE; break; |
| 197 | case Instruction::SetLE: opCode = MOVFLE; break; |
| 198 | case Instruction::SetGE: opCode = MOVFGE; break; |
| 199 | case Instruction::SetLT: opCode = MOVFL; break; |
| 200 | case Instruction::SetGT: opCode = MOVFG; break; |
| 201 | default: |
| 202 | assert(0 && "Unrecognized VM instruction!"); |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | return opCode; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | // Assumes that SUBcc v1, v2 -> v3 has been executed. |
| 211 | // In most cases, we want to clear v3 and then follow it by instruction |
| 212 | // MOVcc 1 -> v3. |
| 213 | // Set mustClearReg=false if v3 need not be cleared before conditional move. |
| 214 | // Set valueToMove=0 if we want to conditionally move 0 instead of 1 |
| 215 | // (i.e., we want to test inverse of a condition) |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 216 | // (The latter two cases do not seem to arise because SetNE needs nothing.) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 217 | // |
| 218 | static MachineOpCode |
| 219 | ChooseMovpccAfterSub(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 220 | bool& mustClearReg, |
| 221 | int& valueToMove) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 222 | { |
| 223 | MachineOpCode opCode = INVALID_OPCODE; |
| 224 | mustClearReg = true; |
| 225 | valueToMove = 1; |
| 226 | |
| 227 | switch(instrNode->getInstruction()->getOpcode()) |
| 228 | { |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 229 | case Instruction::SetEQ: opCode = MOVE; break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 230 | case Instruction::SetLE: opCode = MOVLE; break; |
| 231 | case Instruction::SetGE: opCode = MOVGE; break; |
| 232 | case Instruction::SetLT: opCode = MOVL; break; |
| 233 | case Instruction::SetGT: opCode = MOVG; break; |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 234 | case Instruction::SetNE: assert(0 && "No move required!"); break; |
| 235 | default: assert(0 && "Unrecognized VM instr!"); break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | return opCode; |
| 239 | } |
| 240 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 241 | static inline MachineOpCode |
| 242 | ChooseConvertToFloatInstr(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 243 | const Type* opType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 244 | { |
| 245 | MachineOpCode opCode = INVALID_OPCODE; |
| 246 | |
| 247 | switch(instrNode->getOpLabel()) |
| 248 | { |
| 249 | case ToFloatTy: |
| 250 | if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 251 | opCode = FITOS; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 252 | else if (opType == Type::LongTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 253 | opCode = FXTOS; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 254 | else if (opType == Type::DoubleTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 255 | opCode = FDTOS; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 256 | else if (opType == Type::FloatTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 257 | ; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 258 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 259 | assert(0 && "Cannot convert this type to FLOAT on SPARC"); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
| 262 | case ToDoubleTy: |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 263 | // This is usually used in conjunction with CreateCodeToCopyIntToFloat(). |
| 264 | // Both functions should treat the integer as a 32-bit value for types |
| 265 | // of 4 bytes or less, and as a 64-bit value otherwise. |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 266 | if (opType == Type::SByteTy || opType == Type::UByteTy || |
| 267 | opType == Type::ShortTy || opType == Type::UShortTy || |
| 268 | opType == Type::IntTy || opType == Type::UIntTy) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 269 | opCode = FITOD; |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 270 | else if (opType == Type::LongTy || opType == Type::ULongTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 271 | opCode = FXTOD; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 272 | else if (opType == Type::FloatTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 273 | opCode = FSTOD; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 274 | else if (opType == Type::DoubleTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 275 | ; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 276 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 277 | assert(0 && "Cannot convert this type to DOUBLE on SPARC"); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 278 | break; |
| 279 | |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | return opCode; |
| 285 | } |
| 286 | |
| 287 | static inline MachineOpCode |
| 288 | ChooseConvertToIntInstr(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 289 | const Type* opType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 290 | { |
| 291 | MachineOpCode opCode = INVALID_OPCODE;; |
| 292 | |
| 293 | int instrType = (int) instrNode->getOpLabel(); |
| 294 | |
| 295 | if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy) |
| 296 | { |
| 297 | switch (opType->getPrimitiveID()) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 298 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 299 | case Type::FloatTyID: opCode = FSTOI; break; |
| 300 | case Type::DoubleTyID: opCode = FDTOI; break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 301 | default: |
| 302 | assert(0 && "Non-numeric non-bool type cannot be converted to Int"); |
| 303 | break; |
| 304 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 305 | } |
| 306 | else if (instrType == ToLongTy) |
| 307 | { |
| 308 | switch (opType->getPrimitiveID()) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 309 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 310 | case Type::FloatTyID: opCode = FSTOX; break; |
| 311 | case Type::DoubleTyID: opCode = FDTOX; break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 312 | default: |
| 313 | assert(0 && "Non-numeric non-bool type cannot be converted to Long"); |
| 314 | break; |
| 315 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | assert(0 && "Should not get here, Mo!"); |
| 319 | |
| 320 | return opCode; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | static inline MachineOpCode |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 325 | ChooseAddInstructionByType(const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 326 | { |
| 327 | MachineOpCode opCode = INVALID_OPCODE; |
| 328 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 329 | if (resultType->isIntegral() || |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 330 | resultType->isPointerType() || |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 331 | resultType->isLabelType() || |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 332 | isa<MethodType>(resultType) || |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 333 | resultType == Type::BoolTy) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 334 | { |
| 335 | opCode = ADD; |
| 336 | } |
| 337 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 338 | switch(resultType->getPrimitiveID()) |
| 339 | { |
| 340 | case Type::FloatTyID: opCode = FADDS; break; |
| 341 | case Type::DoubleTyID: opCode = FADDD; break; |
| 342 | default: assert(0 && "Invalid type for ADD instruction"); break; |
| 343 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 344 | |
| 345 | return opCode; |
| 346 | } |
| 347 | |
| 348 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 349 | static inline MachineOpCode |
| 350 | ChooseAddInstruction(const InstructionNode* instrNode) |
| 351 | { |
| 352 | return ChooseAddInstructionByType(instrNode->getInstruction()->getType()); |
| 353 | } |
| 354 | |
| 355 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 356 | static inline MachineInstr* |
| 357 | CreateMovFloatInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 358 | const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 359 | { |
| 360 | MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 361 | ? FMOVS : FMOVD); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 362 | minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 363 | instrNode->leftChild()->getValue()); |
| 364 | minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, |
| 365 | instrNode->getValue()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 366 | return minstr; |
| 367 | } |
| 368 | |
| 369 | static inline MachineInstr* |
| 370 | CreateAddConstInstruction(const InstructionNode* instrNode) |
| 371 | { |
| 372 | MachineInstr* minstr = NULL; |
| 373 | |
| 374 | Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue(); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 375 | assert(isa<Constant>(constOp)); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 376 | |
| 377 | // Cases worth optimizing are: |
| 378 | // (1) Add with 0 for float or double: use an FMOV of appropriate type, |
| 379 | // instead of an FADD (1 vs 3 cycles). There is no integer MOV. |
| 380 | // |
| 381 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 382 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 383 | if (resultType == Type::FloatTy || |
| 384 | resultType == Type::DoubleTy) |
| 385 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 386 | double dval = cast<ConstantFP>(constOp)->getValue(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 387 | if (dval == 0.0) |
| 388 | minstr = CreateMovFloatInstruction(instrNode, resultType); |
| 389 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 390 | |
| 391 | return minstr; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | static inline MachineOpCode |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 396 | ChooseSubInstructionByType(const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 397 | { |
| 398 | MachineOpCode opCode = INVALID_OPCODE; |
| 399 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 400 | if (resultType->isIntegral() || |
| 401 | resultType->isPointerType()) |
| 402 | { |
| 403 | opCode = SUB; |
| 404 | } |
| 405 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 406 | switch(resultType->getPrimitiveID()) |
| 407 | { |
| 408 | case Type::FloatTyID: opCode = FSUBS; break; |
| 409 | case Type::DoubleTyID: opCode = FSUBD; break; |
| 410 | default: assert(0 && "Invalid type for SUB instruction"); break; |
| 411 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 412 | |
| 413 | return opCode; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | static inline MachineInstr* |
| 418 | CreateSubConstInstruction(const InstructionNode* instrNode) |
| 419 | { |
| 420 | MachineInstr* minstr = NULL; |
| 421 | |
| 422 | Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue(); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 423 | assert(isa<Constant>(constOp)); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 424 | |
| 425 | // Cases worth optimizing are: |
| 426 | // (1) Sub with 0 for float or double: use an FMOV of appropriate type, |
| 427 | // instead of an FSUB (1 vs 3 cycles). There is no integer MOV. |
| 428 | // |
| 429 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 430 | |
| 431 | if (resultType == Type::FloatTy || |
| 432 | resultType == Type::DoubleTy) |
| 433 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 434 | double dval = cast<ConstantFP>(constOp)->getValue(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 435 | if (dval == 0.0) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 436 | minstr = CreateMovFloatInstruction(instrNode, resultType); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return minstr; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | static inline MachineOpCode |
| 444 | ChooseFcmpInstruction(const InstructionNode* instrNode) |
| 445 | { |
| 446 | MachineOpCode opCode = INVALID_OPCODE; |
| 447 | |
| 448 | Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue(); |
| 449 | switch(operand->getType()->getPrimitiveID()) { |
| 450 | case Type::FloatTyID: opCode = FCMPS; break; |
| 451 | case Type::DoubleTyID: opCode = FCMPD; break; |
| 452 | default: assert(0 && "Invalid type for FCMP instruction"); break; |
| 453 | } |
| 454 | |
| 455 | return opCode; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | // Assumes that leftArg and rightArg are both cast instructions. |
| 460 | // |
| 461 | static inline bool |
| 462 | BothFloatToDouble(const InstructionNode* instrNode) |
| 463 | { |
| 464 | InstrTreeNode* leftArg = instrNode->leftChild(); |
| 465 | InstrTreeNode* rightArg = instrNode->rightChild(); |
| 466 | InstrTreeNode* leftArgArg = leftArg->leftChild(); |
| 467 | InstrTreeNode* rightArgArg = rightArg->leftChild(); |
| 468 | assert(leftArg->getValue()->getType() == rightArg->getValue()->getType()); |
| 469 | |
| 470 | // Check if both arguments are floats cast to double |
| 471 | return (leftArg->getValue()->getType() == Type::DoubleTy && |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 472 | leftArgArg->getValue()->getType() == Type::FloatTy && |
| 473 | rightArgArg->getValue()->getType() == Type::FloatTy); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | |
| 477 | static inline MachineOpCode |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 478 | ChooseMulInstructionByType(const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 479 | { |
| 480 | MachineOpCode opCode = INVALID_OPCODE; |
| 481 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 482 | if (resultType->isIntegral()) |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 483 | opCode = MULX; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 484 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 485 | switch(resultType->getPrimitiveID()) |
| 486 | { |
| 487 | case Type::FloatTyID: opCode = FMULS; break; |
| 488 | case Type::DoubleTyID: opCode = FMULD; break; |
| 489 | default: assert(0 && "Invalid type for MUL instruction"); break; |
| 490 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 491 | |
| 492 | return opCode; |
| 493 | } |
| 494 | |
| 495 | |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 497 | static inline MachineInstr* |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 498 | CreateIntNegInstruction(const TargetMachine& target, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 499 | Value* vreg) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 500 | { |
| 501 | MachineInstr* minstr = new MachineInstr(SUB); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 502 | minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum()); |
| 503 | minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg); |
| 504 | minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 505 | return minstr; |
| 506 | } |
| 507 | |
| 508 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 509 | // Does not create any instructions if we cannot exploit constant to |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 510 | // create a cheaper instruction. |
| 511 | // This returns the approximate cost of the instructions generated, |
| 512 | // which is used to pick the cheapest when both operands are constant. |
| 513 | static inline unsigned int |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 514 | CreateMulConstInstruction(const TargetMachine &target, |
| 515 | Value* lval, Value* rval, Value* destVal, |
| 516 | vector<MachineInstr*>& mvec) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 517 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 518 | /* An integer multiply is generally more costly than FP multiply */ |
| 519 | unsigned int cost = target.getInstrInfo().minLatency(MULX); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 520 | MachineInstr* minstr1 = NULL; |
| 521 | MachineInstr* minstr2 = NULL; |
| 522 | |
| 523 | Value* constOp = rval; |
| 524 | if (! isa<Constant>(constOp)) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 525 | return cost; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 526 | |
| 527 | // Cases worth optimizing are: |
| 528 | // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV) |
| 529 | // (2) Multiply by 2^x for integer types: replace with Shift |
| 530 | // |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 531 | const Type* resultType = destVal->getType(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 532 | |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 533 | if (resultType->isIntegral() || resultType->isPointerType()) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 534 | { |
| 535 | unsigned pow; |
| 536 | bool isValidConst; |
| 537 | int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst); |
| 538 | if (isValidConst) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 539 | { |
| 540 | bool needNeg = false; |
| 541 | if (C < 0) |
| 542 | { |
| 543 | needNeg = true; |
| 544 | C = -C; |
| 545 | } |
| 546 | |
| 547 | if (C == 0 || C == 1) |
| 548 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 549 | cost = target.getInstrInfo().minLatency(ADD); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 550 | minstr1 = new MachineInstr(ADD); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 551 | if (C == 0) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 552 | minstr1->SetMachineOperandReg(0, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 553 | target.getRegInfo().getZeroRegNum()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 554 | else |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 555 | minstr1->SetMachineOperandVal(0, |
| 556 | MachineOperand::MO_VirtualRegister, lval); |
| 557 | minstr1->SetMachineOperandReg(1, |
| 558 | target.getRegInfo().getZeroRegNum()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 559 | } |
| 560 | else if (IsPowerOf2(C, pow)) |
| 561 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 562 | minstr1 = new MachineInstr((resultType == Type::LongTy) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 563 | ? SLLX : SLL); |
| 564 | minstr1->SetMachineOperandVal(0, |
| 565 | MachineOperand::MO_VirtualRegister, lval); |
| 566 | minstr1->SetMachineOperandConst(1, |
| 567 | MachineOperand::MO_UnextendedImmed, pow); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 570 | if (minstr1 && needNeg) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 571 | { // insert <reg = SUB 0, reg> after the instr to flip the sign |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 572 | minstr2 = CreateIntNegInstruction(target, destVal); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 573 | cost += target.getInstrInfo().minLatency(minstr2->getOpCode()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 576 | } |
| 577 | else |
| 578 | { |
| 579 | if (resultType == Type::FloatTy || |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 580 | resultType == Type::DoubleTy) |
| 581 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 582 | double dval = cast<ConstantFP>(constOp)->getValue(); |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 583 | if (fabs(dval) == 1) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 584 | { |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 585 | bool needNeg = (dval < 0); |
| 586 | |
| 587 | MachineOpCode opCode = needNeg |
| 588 | ? (resultType == Type::FloatTy? FNEGS : FNEGD) |
| 589 | : (resultType == Type::FloatTy? FMOVS : FMOVD); |
| 590 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 591 | minstr1 = new MachineInstr(opCode); |
| 592 | minstr1->SetMachineOperandVal(0, |
| 593 | MachineOperand::MO_VirtualRegister, |
| 594 | lval); |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 595 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 599 | if (minstr1 != NULL) |
| 600 | minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, |
| 601 | destVal); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 602 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 603 | if (minstr1) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 604 | { |
| 605 | mvec.push_back(minstr1); |
| 606 | cost = target.getInstrInfo().minLatency(minstr1->getOpCode()); |
| 607 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 608 | if (minstr2) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 609 | { |
| 610 | assert(minstr1 && "Otherwise cost needs to be initialized to 0"); |
| 611 | cost += target.getInstrInfo().minLatency(minstr2->getOpCode()); |
| 612 | mvec.push_back(minstr2); |
| 613 | } |
| 614 | |
| 615 | return cost; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 619 | // Does not create any instructions if we cannot exploit constant to |
| 620 | // create a cheaper instruction. |
| 621 | // |
| 622 | static inline void |
| 623 | CreateCheapestMulConstInstruction(const TargetMachine &target, |
| 624 | Value* lval, Value* rval, Value* destVal, |
| 625 | vector<MachineInstr*>& mvec) |
| 626 | { |
| 627 | Value* constOp; |
| 628 | if (isa<Constant>(lval) && isa<Constant>(rval)) |
| 629 | { // both operands are constant: try both orders! |
| 630 | vector<MachineInstr*> mvec1, mvec2; |
| 631 | unsigned int lcost = CreateMulConstInstruction(target, lval, rval, |
| 632 | destVal, mvec1); |
| 633 | unsigned int rcost = CreateMulConstInstruction(target, rval, lval, |
| 634 | destVal, mvec2); |
| 635 | vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2; |
| 636 | vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1; |
| 637 | mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end()); |
| 638 | |
| 639 | for (unsigned int i=0; i < maxcostMvec.size(); ++i) |
| 640 | delete maxcostMvec[i]; |
| 641 | } |
| 642 | else if (isa<Constant>(rval)) // rval is constant, but not lval |
| 643 | CreateMulConstInstruction(target, lval, rval, destVal, mvec); |
| 644 | else if (isa<Constant>(lval)) // lval is constant, but not rval |
| 645 | CreateMulConstInstruction(target, lval, rval, destVal, mvec); |
| 646 | |
| 647 | // else neither is constant |
| 648 | return; |
| 649 | } |
| 650 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 651 | // Return NULL if we cannot exploit constant to create a cheaper instruction |
| 652 | static inline void |
| 653 | CreateMulInstruction(const TargetMachine &target, |
| 654 | Value* lval, Value* rval, Value* destVal, |
| 655 | vector<MachineInstr*>& mvec, |
| 656 | MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE) |
| 657 | { |
| 658 | unsigned int L = mvec.size(); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 659 | CreateCheapestMulConstInstruction(target, lval, rval, destVal, mvec); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 660 | if (mvec.size() == L) |
| 661 | { // no instructions were added so create MUL reg, reg, reg. |
| 662 | // Use FSMULD if both operands are actually floats cast to doubles. |
| 663 | // Otherwise, use the default opcode for the appropriate type. |
| 664 | MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE) |
| 665 | ? forceMulOp |
| 666 | : ChooseMulInstructionByType(destVal->getType())); |
| 667 | MachineInstr* M = new MachineInstr(mulOp); |
| 668 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval); |
| 669 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval); |
| 670 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal); |
| 671 | mvec.push_back(M); |
| 672 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 676 | // Generate a divide instruction for Div or Rem. |
| 677 | // For Rem, this assumes that the operand type will be signed if the result |
| 678 | // type is signed. This is correct because they must have the same sign. |
| 679 | // |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 680 | static inline MachineOpCode |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 681 | ChooseDivInstruction(TargetMachine &target, |
| 682 | const InstructionNode* instrNode) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 683 | { |
| 684 | MachineOpCode opCode = INVALID_OPCODE; |
| 685 | |
| 686 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 687 | |
| 688 | if (resultType->isIntegral()) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 689 | opCode = resultType->isSigned()? SDIVX : UDIVX; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 690 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 691 | switch(resultType->getPrimitiveID()) |
| 692 | { |
| 693 | case Type::FloatTyID: opCode = FDIVS; break; |
| 694 | case Type::DoubleTyID: opCode = FDIVD; break; |
| 695 | default: assert(0 && "Invalid type for DIV instruction"); break; |
| 696 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 697 | |
| 698 | return opCode; |
| 699 | } |
| 700 | |
| 701 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 702 | // Return NULL if we cannot exploit constant to create a cheaper instruction |
| 703 | static inline void |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 704 | CreateDivConstInstruction(TargetMachine &target, |
| 705 | const InstructionNode* instrNode, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 706 | vector<MachineInstr*>& mvec) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 707 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 708 | MachineInstr* minstr1 = NULL; |
| 709 | MachineInstr* minstr2 = NULL; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 710 | |
| 711 | Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue(); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 712 | if (! isa<Constant>(constOp)) |
| 713 | return; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 714 | |
| 715 | // Cases worth optimizing are: |
| 716 | // (1) Divide by 1 for any type: replace with copy (ADD or FMOV) |
| 717 | // (2) Divide by 2^x for integer types: replace with SR[L or A]{X} |
| 718 | // |
| 719 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 720 | |
| 721 | if (resultType->isIntegral()) |
| 722 | { |
| 723 | unsigned pow; |
| 724 | bool isValidConst; |
| 725 | int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst); |
| 726 | if (isValidConst) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 727 | { |
| 728 | bool needNeg = false; |
| 729 | if (C < 0) |
| 730 | { |
| 731 | needNeg = true; |
| 732 | C = -C; |
| 733 | } |
| 734 | |
| 735 | if (C == 1) |
| 736 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 737 | minstr1 = new MachineInstr(ADD); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 738 | minstr1->SetMachineOperandVal(0, |
| 739 | MachineOperand::MO_VirtualRegister, |
| 740 | instrNode->leftChild()->getValue()); |
| 741 | minstr1->SetMachineOperandReg(1, |
| 742 | target.getRegInfo().getZeroRegNum()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 743 | } |
| 744 | else if (IsPowerOf2(C, pow)) |
| 745 | { |
| 746 | MachineOpCode opCode= ((resultType->isSigned()) |
| 747 | ? (resultType==Type::LongTy)? SRAX : SRA |
| 748 | : (resultType==Type::LongTy)? SRLX : SRL); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 749 | minstr1 = new MachineInstr(opCode); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 750 | minstr1->SetMachineOperandVal(0, |
| 751 | MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 752 | instrNode->leftChild()->getValue()); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 753 | minstr1->SetMachineOperandConst(1, |
| 754 | MachineOperand::MO_UnextendedImmed, |
| 755 | pow); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 758 | if (minstr1 && needNeg) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 759 | { // insert <reg = SUB 0, reg> after the instr to flip the sign |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 760 | minstr2 = CreateIntNegInstruction(target, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 761 | instrNode->getValue()); |
| 762 | } |
| 763 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 764 | } |
| 765 | else |
| 766 | { |
| 767 | if (resultType == Type::FloatTy || |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 768 | resultType == Type::DoubleTy) |
| 769 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 770 | double dval = cast<ConstantFP>(constOp)->getValue(); |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 771 | if (fabs(dval) == 1) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 772 | { |
| 773 | bool needNeg = (dval < 0); |
| 774 | |
| 775 | MachineOpCode opCode = needNeg |
| 776 | ? (resultType == Type::FloatTy? FNEGS : FNEGD) |
| 777 | : (resultType == Type::FloatTy? FMOVS : FMOVD); |
| 778 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 779 | minstr1 = new MachineInstr(opCode); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 780 | minstr1->SetMachineOperandVal(0, |
| 781 | MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 782 | instrNode->leftChild()->getValue()); |
| 783 | } |
| 784 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 787 | if (minstr1 != NULL) |
| 788 | minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, |
| 789 | instrNode->getValue()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 790 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 791 | if (minstr1) |
| 792 | mvec.push_back(minstr1); |
| 793 | if (minstr2) |
| 794 | mvec.push_back(minstr2); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 798 | static void |
| 799 | CreateCodeForVariableSizeAlloca(const TargetMachine& target, |
| 800 | Instruction* result, |
| 801 | unsigned int tsize, |
| 802 | Value* numElementsVal, |
| 803 | vector<MachineInstr*>& getMvec) |
| 804 | { |
| 805 | MachineInstr* M; |
| 806 | |
| 807 | // Create a Value to hold the (constant) element size |
| 808 | Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize); |
| 809 | |
| 810 | // Get the constant offset from SP for dynamically allocated storage |
| 811 | // and create a temporary Value to hold it. |
| 812 | assert(result && result->getParent() && "Result value is not part of a method?"); |
| 813 | Method* method = result->getParent()->getParent(); |
| 814 | MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method); |
| 815 | bool growUp; |
| 816 | ConstantSInt* dynamicAreaOffset = |
| 817 | ConstantSInt::get(Type::IntTy, |
| 818 | target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp)); |
| 819 | assert(! growUp && "Has SPARC v9 stack frame convention changed?"); |
| 820 | |
| 821 | // Create a temporary value to hold the result of MUL |
| 822 | TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal); |
| 823 | MachineCodeForInstruction::get(result).addTemp(tmpProd); |
| 824 | |
| 825 | // Instruction 1: mul numElements, typeSize -> tmpProd |
| 826 | M = new MachineInstr(MULX); |
| 827 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal); |
| 828 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal); |
| 829 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd); |
| 830 | getMvec.push_back(M); |
| 831 | |
| 832 | // Instruction 2: sub %sp, tmpProd -> %sp |
| 833 | M = new MachineInstr(SUB); |
| 834 | M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer()); |
| 835 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd); |
| 836 | M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer()); |
| 837 | getMvec.push_back(M); |
| 838 | |
| 839 | // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result |
| 840 | M = new MachineInstr(ADD); |
| 841 | M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer()); |
| 842 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset); |
| 843 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result); |
| 844 | getMvec.push_back(M); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | static void |
| 849 | CreateCodeForFixedSizeAlloca(const TargetMachine& target, |
| 850 | Instruction* result, |
| 851 | unsigned int tsize, |
| 852 | unsigned int numElements, |
| 853 | vector<MachineInstr*>& getMvec) |
| 854 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 855 | assert(result && result->getParent() && |
| 856 | "Result value is not part of a method?"); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 857 | Method* method = result->getParent()->getParent(); |
| 858 | MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method); |
| 859 | |
| 860 | // Check if the offset would small enough to use as an immediate in load/stores |
| 861 | // (check LDX because all load/stores have the same-size immediate field). |
| 862 | // If not, put the variable in the dynamically sized area of the frame. |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 863 | unsigned int paddedSizeIgnored; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 864 | int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 865 | paddedSizeIgnored, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 866 | tsize * numElements); |
| 867 | if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP)) |
| 868 | { |
| 869 | CreateCodeForVariableSizeAlloca(target, result, tsize, |
| 870 | ConstantSInt::get(Type::IntTy,numElements), |
| 871 | getMvec); |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | // else offset fits in immediate field so go ahead and allocate it. |
| 876 | offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements); |
| 877 | |
| 878 | // Create a temporary Value to hold the constant offset. |
| 879 | // This is needed because it may not fit in the immediate field. |
| 880 | ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP); |
| 881 | |
| 882 | // Instruction 1: add %fp, offsetFromFP -> result |
| 883 | MachineInstr* M = new MachineInstr(ADD); |
| 884 | M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
| 885 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal); |
| 886 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result); |
| 887 | |
| 888 | getMvec.push_back(M); |
| 889 | } |
| 890 | |
| 891 | |
| 892 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 893 | //------------------------------------------------------------------------ |
| 894 | // Function SetOperandsForMemInstr |
| 895 | // |
| 896 | // Choose addressing mode for the given load or store instruction. |
| 897 | // Use [reg+reg] if it is an indexed reference, and the index offset is |
| 898 | // not a constant or if it cannot fit in the offset field. |
| 899 | // Use [reg+offset] in all other cases. |
| 900 | // |
| 901 | // This assumes that all array refs are "lowered" to one of these forms: |
| 902 | // %x = load (subarray*) ptr, constant ; single constant offset |
| 903 | // %x = load (subarray*) ptr, offsetVal ; single non-constant offset |
| 904 | // Generally, this should happen via strength reduction + LICM. |
| 905 | // Also, strength reduction should take care of using the same register for |
| 906 | // the loop index variable and an array index, when that is profitable. |
| 907 | //------------------------------------------------------------------------ |
| 908 | |
| 909 | static void |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 910 | SetOperandsForMemInstr(vector<MachineInstr*>& mvec, |
| 911 | vector<MachineInstr*>::iterator mvecI, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 912 | const InstructionNode* vmInstrNode, |
| 913 | const TargetMachine& target) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 914 | { |
| 915 | MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction(); |
| 916 | |
| 917 | // Variables to hold the index vector, ptr value, and offset value. |
| 918 | // The major work here is to extract these for all 3 instruction types |
| 919 | // and then call the common function SetMemOperands_Internal(). |
| 920 | // |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 921 | Value* ptrVal = memInst->getPointerOperand(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 922 | |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 923 | // Start with the index vector of this instruction, if any. |
| 924 | vector<Value*> idxVec; |
| 925 | idxVec.insert(idxVec.end(), memInst->idx_begin(), memInst->idx_end()); |
| 926 | |
| 927 | // If there is a GetElemPtr instruction to fold in to this instr, |
| 928 | // it must be in the left child for Load and GetElemPtr, and in the |
| 929 | // right child for Store instructions. |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 930 | InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 931 | ? vmInstrNode->rightChild() |
| 932 | : vmInstrNode->leftChild()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 933 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 934 | // Fold chains of GetElemPtr instructions for structure references. |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 935 | if (isa<StructType>(cast<PointerType>(ptrVal->getType())->getElementType()) |
| 936 | && (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 937 | ptrChild->getOpLabel() == GetElemPtrIdx)) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 938 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 939 | Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec); |
| 940 | if (newPtr) |
| 941 | ptrVal = newPtr; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 944 | SetMemOperands_Internal(mvec, mvecI, vmInstrNode, ptrVal, idxVec, target); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 948 | // Generate the correct operands (and additional instructions if needed) |
| 949 | // for the given pointer and given index vector. |
| 950 | // |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 951 | static void |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 952 | SetMemOperands_Internal(vector<MachineInstr*>& mvec, |
| 953 | vector<MachineInstr*>::iterator mvecI, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 954 | const InstructionNode* vmInstrNode, |
| 955 | Value* ptrVal, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 956 | vector<Value*>& idxVec, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 957 | const TargetMachine& target) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 958 | { |
| 959 | MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction(); |
| 960 | |
| 961 | // Initialize so we default to storing the offset in a register. |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 962 | int64_t smallConstOffset = 0; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 963 | Value* valueForRegOffset = NULL; |
| 964 | MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister; |
| 965 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 966 | // Check if there is an index vector and if so, compute the |
| 967 | // right offset for structures and for arrays |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 968 | // |
| 969 | if (idxVec.size() > 0) |
| 970 | { |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 971 | unsigned offset = 0; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 972 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 973 | const PointerType* ptrType = cast<PointerType>(ptrVal->getType()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 974 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 975 | // Handle special common case of leading [0] index. |
| 976 | bool firstIndexIsZero = |
| 977 | bool(isa<ConstantUInt>(idxVec.front()) && |
| 978 | cast<ConstantUInt>(idxVec.front())->getValue() == 0); |
| 979 | |
| 980 | // This is a real structure reference if the ptr target is a |
| 981 | // structure type, and the first offset is [0] (eliminate that offset). |
| 982 | if (firstIndexIsZero && ptrType->getElementType()->isStructType()) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 983 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 984 | // Compute the offset value using the index vector. Create a |
| 985 | // virtual reg. for it since it may not fit in the immed field. |
| 986 | assert(idxVec.size() >= 2); |
| 987 | idxVec.erase(idxVec.begin()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 988 | unsigned offset = target.DataLayout.getIndexedOffset(ptrType,idxVec); |
| 989 | valueForRegOffset = ConstantSInt::get(Type::IntTy, offset); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 991 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 992 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 993 | // It is an array ref, and must have been lowered to a single offset. |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 994 | assert((memInst->getNumOperands() |
| 995 | == (unsigned) 1 + memInst->getFirstIndexOperandNumber()) |
| 996 | && "Array refs must be lowered before Instruction Selection"); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 997 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 998 | Value* arrayOffsetVal = * memInst->idx_begin(); |
| 999 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1000 | // If index is 0, the offset value is just 0. Otherwise, |
| 1001 | // generate a MUL instruction to compute address from index. |
| 1002 | // The call to getTypeSize() will fail if size is not constant. |
| 1003 | // CreateMulInstruction() folds constants intelligently enough. |
| 1004 | // |
| 1005 | if (firstIndexIsZero) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1006 | { |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1007 | offsetOpType = MachineOperand::MO_SignExtendedImmed; |
| 1008 | smallConstOffset = 0; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1009 | } |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1010 | else |
| 1011 | { |
| 1012 | vector<MachineInstr*> mulVec; |
| 1013 | Instruction* addr = new TmpInstruction(Type::UIntTy, memInst); |
| 1014 | MachineCodeForInstruction::get(memInst).addTemp(addr); |
| 1015 | |
| 1016 | unsigned int eltSize = |
| 1017 | target.DataLayout.getTypeSize(ptrType->getElementType()); |
| 1018 | assert(eltSize > 0 && "Invalid or non-const array element size"); |
| 1019 | ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize); |
| 1020 | |
| 1021 | CreateMulInstruction(target, |
| 1022 | arrayOffsetVal, /* lval, not likely const */ |
| 1023 | eltVal, /* rval, likely constant */ |
| 1024 | addr, /* result*/ |
| 1025 | mulVec, INVALID_MACHINE_OPCODE); |
| 1026 | assert(mulVec.size() > 0 && "No multiply instruction created?"); |
| 1027 | for (vector<MachineInstr*>::const_iterator I = mulVec.begin(); |
| 1028 | I != mulVec.end(); ++I) |
| 1029 | { |
| 1030 | mvecI = mvec.insert(mvecI, *I); // ptr to inserted value |
| 1031 | ++mvecI; // ptr to mem. instr. |
| 1032 | } |
| 1033 | |
| 1034 | valueForRegOffset = addr; |
| 1035 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1036 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | offsetOpType = MachineOperand::MO_SignExtendedImmed; |
| 1041 | smallConstOffset = 0; |
| 1042 | } |
| 1043 | |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 1044 | // For STORE: |
| 1045 | // Operand 0 is value, operand 1 is ptr, operand 2 is offset |
| 1046 | // For LOAD or GET_ELEMENT_PTR, |
| 1047 | // Operand 0 is ptr, operand 1 is offset, operand 2 is result. |
| 1048 | // |
| 1049 | unsigned offsetOpNum, ptrOpNum; |
| 1050 | if (memInst->getOpcode() == Instruction::Store) |
| 1051 | { |
| 1052 | (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 1053 | vmInstrNode->leftChild()->getValue()); |
| 1054 | ptrOpNum = 1; |
| 1055 | offsetOpNum = 2; |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | ptrOpNum = 0; |
| 1060 | offsetOpNum = 1; |
| 1061 | (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, |
| 1062 | memInst); |
| 1063 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1064 | |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 1065 | (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister, |
| 1066 | ptrVal); |
| 1067 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1068 | if (offsetOpType == MachineOperand::MO_VirtualRegister) |
| 1069 | { |
| 1070 | assert(valueForRegOffset != NULL); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1071 | (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType, |
| 1072 | valueForRegOffset); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1073 | } |
| 1074 | else |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1075 | (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType, |
| 1076 | smallConstOffset); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1080 | // |
| 1081 | // Substitute operand `operandNum' of the instruction in node `treeNode' |
Vikram S. Adve | c025fc1 | 2001-10-14 23:28:43 +0000 | [diff] [blame] | 1082 | // in place of the use(s) of that instruction in node `parent'. |
| 1083 | // Check both explicit and implicit operands! |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1084 | // Also make sure to skip over a parent who: |
| 1085 | // (1) is a list node in the Burg tree, or |
| 1086 | // (2) itself had its results forwarded to its parent |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1087 | // |
| 1088 | static void |
| 1089 | ForwardOperand(InstructionNode* treeNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1090 | InstrTreeNode* parent, |
| 1091 | int operandNum) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1092 | { |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 1093 | assert(treeNode && parent && "Invalid invocation of ForwardOperand"); |
| 1094 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1095 | Instruction* unusedOp = treeNode->getInstruction(); |
| 1096 | Value* fwdOp = unusedOp->getOperand(operandNum); |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 1097 | |
| 1098 | // The parent itself may be a list node, so find the real parent instruction |
| 1099 | while (parent->getNodeType() != InstrTreeNode::NTInstructionNode) |
| 1100 | { |
| 1101 | parent = parent->parent(); |
| 1102 | assert(parent && "ERROR: Non-instruction node has no parent in tree."); |
| 1103 | } |
| 1104 | InstructionNode* parentInstrNode = (InstructionNode*) parent; |
| 1105 | |
| 1106 | Instruction* userInstr = parentInstrNode->getInstruction(); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1107 | MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1108 | |
| 1109 | // The parent's mvec would be empty if it was itself forwarded. |
| 1110 | // Recursively call ForwardOperand in that case... |
| 1111 | // |
| 1112 | if (mvec.size() == 0) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1113 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1114 | assert(parent->parent() != NULL && |
| 1115 | "Parent could not have been forwarded, yet has no instructions?"); |
| 1116 | ForwardOperand(treeNode, parent->parent(), operandNum); |
| 1117 | } |
| 1118 | else |
| 1119 | { |
| 1120 | bool fwdSuccessful = false; |
| 1121 | for (unsigned i=0, N=mvec.size(); i < N; i++) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1122 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1123 | MachineInstr* minstr = mvec[i]; |
| 1124 | for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1125 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1126 | const MachineOperand& mop = minstr->getOperand(i); |
| 1127 | if (mop.getOperandType() == MachineOperand::MO_VirtualRegister && |
| 1128 | mop.getVRegValue() == unusedOp) |
| 1129 | { |
| 1130 | minstr->SetMachineOperandVal(i, |
| 1131 | MachineOperand::MO_VirtualRegister, fwdOp); |
| 1132 | fwdSuccessful = true; |
| 1133 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1134 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1135 | |
| 1136 | for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i) |
| 1137 | if (minstr->getImplicitRef(i) == unusedOp) |
| 1138 | { |
| 1139 | minstr->setImplicitRef(i, fwdOp, |
| 1140 | minstr->implicitRefIsDefined(i)); |
| 1141 | fwdSuccessful = true; |
| 1142 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1143 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1144 | assert(fwdSuccessful && "Value to be forwarded is never used!"); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1149 | void UltraSparcInstrInfo:: |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1150 | CreateCopyInstructionsByType(const TargetMachine& target, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1151 | Method* method, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1152 | Value* src, |
| 1153 | Instruction* dest, |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1154 | vector<MachineInstr*>& minstrVec) const |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1155 | { |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1156 | bool loadConstantToReg = false; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1157 | |
| 1158 | const Type* resultType = dest->getType(); |
| 1159 | |
| 1160 | MachineOpCode opCode = ChooseAddInstructionByType(resultType); |
| 1161 | if (opCode == INVALID_OPCODE) |
| 1162 | { |
| 1163 | assert(0 && "Unsupported result type in CreateCopyInstructionsByType()"); |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1164 | return; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1167 | // if `src' is a constant that doesn't fit in the immed field or if it is |
| 1168 | // a global variable (i.e., a constant address), generate a load |
| 1169 | // instruction instead of an add |
| 1170 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1171 | if (isa<Constant>(src)) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1172 | { |
| 1173 | unsigned int machineRegNum; |
| 1174 | int64_t immedValue; |
| 1175 | MachineOperand::MachineOperandType opType = |
| 1176 | ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true, |
| 1177 | machineRegNum, immedValue); |
| 1178 | |
| 1179 | if (opType == MachineOperand::MO_VirtualRegister) |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1180 | loadConstantToReg = true; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1181 | } |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1182 | else if (isa<GlobalValue>(src)) |
| 1183 | loadConstantToReg = true; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1184 | |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1185 | if (loadConstantToReg) |
| 1186 | { // `src' is constant and cannot fit in immed field for the ADD |
| 1187 | // Insert instructions to "load" the constant into a register |
| 1188 | vector<TmpInstruction*> tempVec; |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1189 | target.getInstrInfo().CreateCodeToLoadConst(method, src, dest, |
| 1190 | minstrVec,tempVec); |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1191 | for (unsigned i=0; i < tempVec.size(); i++) |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1192 | MachineCodeForInstruction::get(dest).addTemp(tempVec[i]); |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1193 | } |
| 1194 | else |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1195 | { // Create an add-with-0 instruction of the appropriate type. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1196 | // Make `src' the second operand, in case it is a constant |
| 1197 | // Use (unsigned long) 0 for a NULL pointer value. |
| 1198 | // |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1199 | const Type* zeroValueType = |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1200 | (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy |
| 1201 | : resultType; |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1202 | MachineInstr* minstr = new MachineInstr(opCode); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1203 | minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 1204 | Constant::getNullConstant(zeroValueType)); |
| 1205 | minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, src); |
| 1206 | minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest); |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 1207 | minstrVec.push_back(minstr); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1208 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1212 | |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1213 | //******************* Externally Visible Functions *************************/ |
| 1214 | |
| 1215 | |
| 1216 | //------------------------------------------------------------------------ |
| 1217 | // External Function: GetInstructionsForProlog |
| 1218 | // External Function: GetInstructionsForEpilog |
| 1219 | // |
| 1220 | // Purpose: |
| 1221 | // Create prolog and epilog code for procedure entry and exit |
| 1222 | //------------------------------------------------------------------------ |
| 1223 | |
| 1224 | extern unsigned |
| 1225 | GetInstructionsForProlog(BasicBlock* entryBB, |
| 1226 | TargetMachine &target, |
| 1227 | MachineInstr** mvec) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1228 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1229 | MachineInstr* M; |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1230 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1231 | unsigned int N = 0; |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1232 | |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1233 | // The second operand is the stack size. If it does not fit in the |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1234 | // immediate field, we have to use a free register to hold the size. |
| 1235 | // We will assume that local register `l0' is unused since the SAVE |
| 1236 | // instruction must be the first instruction in each procedure. |
| 1237 | // |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1238 | Method* method = entryBB->getParent(); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1239 | MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method); |
| 1240 | unsigned int staticStackSize = mcInfo.getStaticStackSize(); |
| 1241 | |
| 1242 | if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize()) |
| 1243 | staticStackSize = (unsigned) frameInfo.getMinStackFrameSize(); |
| 1244 | |
| 1245 | if (unsigned padsz = (staticStackSize % |
| 1246 | (unsigned) frameInfo.getStackFrameSizeAlignment())) |
Vikram S. Adve | fd9b7dc | 2001-11-12 05:16:39 +0000 | [diff] [blame] | 1247 | staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1248 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1249 | if (target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)) |
| 1250 | { |
| 1251 | M = new MachineInstr(SAVE); |
| 1252 | M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer()); |
| 1253 | M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, |
| 1254 | - (int) staticStackSize); |
| 1255 | M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer()); |
| 1256 | mvec[N++] = M; |
| 1257 | } |
| 1258 | else |
| 1259 | { |
| 1260 | M = new MachineInstr(SETSW); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1261 | M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, |
| 1262 | - (int) staticStackSize); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1263 | M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister, |
| 1264 | target.getRegInfo().getUnifiedRegNum( |
| 1265 | target.getRegInfo().getRegClassIDOfType(Type::IntTy), |
| 1266 | SparcIntRegOrder::l0)); |
| 1267 | mvec[N++] = M; |
| 1268 | |
| 1269 | M = new MachineInstr(SAVE); |
| 1270 | M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer()); |
| 1271 | M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister, |
| 1272 | target.getRegInfo().getUnifiedRegNum( |
| 1273 | target.getRegInfo().getRegClassIDOfType(Type::IntTy), |
| 1274 | SparcIntRegOrder::l0)); |
| 1275 | M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer()); |
| 1276 | mvec[N++] = M; |
| 1277 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1278 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1279 | return N; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1283 | extern unsigned |
| 1284 | GetInstructionsForEpilog(BasicBlock* anExitBB, |
| 1285 | TargetMachine &target, |
| 1286 | MachineInstr** mvec) |
| 1287 | { |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1288 | mvec[0] = new MachineInstr(RESTORE); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1289 | mvec[0]->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum()); |
| 1290 | mvec[0]->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1291 | (int64_t)0); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1292 | mvec[0]->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum()); |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1293 | |
| 1294 | return 1; |
| 1295 | } |
| 1296 | |
| 1297 | |
| 1298 | //------------------------------------------------------------------------ |
| 1299 | // External Function: ThisIsAChainRule |
| 1300 | // |
| 1301 | // Purpose: |
| 1302 | // Check if a given BURG rule is a chain rule. |
| 1303 | //------------------------------------------------------------------------ |
| 1304 | |
| 1305 | extern bool |
| 1306 | ThisIsAChainRule(int eruleno) |
| 1307 | { |
| 1308 | switch(eruleno) |
| 1309 | { |
| 1310 | case 111: // stmt: reg |
| 1311 | case 113: // stmt: bool |
| 1312 | case 123: |
| 1313 | case 124: |
| 1314 | case 125: |
| 1315 | case 126: |
| 1316 | case 127: |
| 1317 | case 128: |
| 1318 | case 129: |
| 1319 | case 130: |
| 1320 | case 131: |
| 1321 | case 132: |
| 1322 | case 133: |
| 1323 | case 155: |
| 1324 | case 221: |
| 1325 | case 222: |
| 1326 | case 241: |
| 1327 | case 242: |
| 1328 | case 243: |
| 1329 | case 244: |
Vikram S. Adve | 85e1e9c | 2002-04-01 20:28:48 +0000 | [diff] [blame^] | 1330 | case 321: |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1331 | return true; break; |
| 1332 | |
| 1333 | default: |
| 1334 | return false; break; |
| 1335 | } |
| 1336 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1337 | |
| 1338 | |
| 1339 | //------------------------------------------------------------------------ |
| 1340 | // External Function: GetInstructionsByRule |
| 1341 | // |
| 1342 | // Purpose: |
| 1343 | // Choose machine instructions for the SPARC according to the |
| 1344 | // patterns chosen by the BURG-generated parser. |
| 1345 | //------------------------------------------------------------------------ |
| 1346 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1347 | void |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1348 | GetInstructionsByRule(InstructionNode* subtreeRoot, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1349 | int ruleForNode, |
| 1350 | short* nts, |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1351 | TargetMachine &target, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1352 | vector<MachineInstr*>& mvec) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1353 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1354 | bool checkCast = false; // initialize here to use fall-through |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1355 | int nextRule; |
| 1356 | int forwardOperandNum = -1; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1357 | unsigned int allocaSize = 0; |
| 1358 | MachineInstr* M, *M2; |
| 1359 | unsigned int L; |
| 1360 | |
| 1361 | mvec.clear(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1362 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1363 | // If the code for this instruction was folded into the parent (user), |
| 1364 | // then do nothing! |
| 1365 | if (subtreeRoot->isFoldedIntoParent()) |
| 1366 | return; |
| 1367 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1368 | // |
| 1369 | // Let's check for chain rules outside the switch so that we don't have |
| 1370 | // to duplicate the list of chain rule production numbers here again |
| 1371 | // |
| 1372 | if (ThisIsAChainRule(ruleForNode)) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1373 | { |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1374 | // Chain rules have a single nonterminal on the RHS. |
| 1375 | // Get the rule that matches the RHS non-terminal and use that instead. |
| 1376 | // |
| 1377 | assert(nts[0] && ! nts[1] |
| 1378 | && "A chain rule should have only one RHS non-terminal!"); |
| 1379 | nextRule = burm_rule(subtreeRoot->state, nts[0]); |
| 1380 | nts = burm_nts[nextRule]; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1381 | GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1382 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1383 | else |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1384 | { |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1385 | switch(ruleForNode) { |
| 1386 | case 1: // stmt: Ret |
| 1387 | case 2: // stmt: RetValue(reg) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1388 | { // NOTE: Prepass of register allocation is responsible |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1389 | // for moving return value to appropriate register. |
| 1390 | // Mark the return-address register as a hidden virtual reg. |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 1391 | // Mark the return value register as an implicit ref of |
| 1392 | // the machine instruction. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1393 | // Finally put a NOP in the delay slot. |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1394 | ReturnInst *returnInstr = |
| 1395 | cast<ReturnInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1396 | assert(returnInstr->getOpcode() == Instruction::Ret); |
| 1397 | |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1398 | Instruction* returnReg = new TmpInstruction(returnInstr); |
| 1399 | MachineCodeForInstruction::get(returnInstr).addTemp(returnReg); |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1400 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1401 | M = new MachineInstr(JMPLRET); |
| 1402 | M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1403 | returnReg); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1404 | M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1405 | (int64_t)8); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1406 | M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1407 | |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 1408 | if (returnInstr->getReturnValue() != NULL) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1409 | M->addImplicitRef(returnInstr->getReturnValue()); |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 1410 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1411 | mvec.push_back(M); |
| 1412 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1413 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1414 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1415 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1416 | |
| 1417 | case 3: // stmt: Store(reg,reg) |
| 1418 | case 4: // stmt: Store(reg,ptrreg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1419 | mvec.push_back(new MachineInstr( |
| 1420 | ChooseStoreInstruction( |
| 1421 | subtreeRoot->leftChild()->getValue()->getType()))); |
| 1422 | SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1423 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1424 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1425 | case 5: // stmt: BrUncond |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1426 | M = new MachineInstr(BA); |
| 1427 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1428 | (Value*)NULL); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1429 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1430 | cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1431 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1432 | |
| 1433 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1434 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1435 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1436 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1437 | case 206: // stmt: BrCond(setCCconst) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1438 | { // setCCconst => boolean was computed with `%b = setCC type reg1 const' |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1439 | // If the constant is ZERO, we can use the branch-on-integer-register |
| 1440 | // instructions and avoid the SUBcc instruction entirely. |
| 1441 | // Otherwise this is just the same as case 5, so just fall through. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1442 | // |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1443 | InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild(); |
| 1444 | assert(constNode && |
| 1445 | constNode->getNodeType() ==InstrTreeNode::NTConstNode); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1446 | Constant *constVal = cast<Constant>(constNode->getValue()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1447 | bool isValidConst; |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1448 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1449 | if ((constVal->getType()->isIntegral() |
| 1450 | || constVal->getType()->isPointerType()) |
| 1451 | && GetConstantValueAsSignedInt(constVal, isValidConst) == 0 |
| 1452 | && isValidConst) |
| 1453 | { |
| 1454 | // That constant is a zero after all... |
| 1455 | // Use the left child of setCC as the first argument! |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1456 | // Mark the setCC node so that no code is generated for it. |
| 1457 | InstructionNode* setCCNode = (InstructionNode*) |
| 1458 | subtreeRoot->leftChild(); |
| 1459 | assert(setCCNode->getOpLabel() == SetCCOp); |
| 1460 | setCCNode->markFoldedIntoParent(); |
| 1461 | |
| 1462 | BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction()); |
| 1463 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1464 | M = new MachineInstr(ChooseBprInstruction(subtreeRoot)); |
| 1465 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1466 | setCCNode->leftChild()->getValue()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1467 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
| 1468 | brInst->getSuccessor(0)); |
| 1469 | mvec.push_back(M); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1470 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1471 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1472 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1473 | |
| 1474 | // false branch |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1475 | M = new MachineInstr(BA); |
| 1476 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
| 1477 | (Value*) NULL); |
| 1478 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1479 | brInst->getSuccessor(1)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1480 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1481 | |
| 1482 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1483 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1484 | |
| 1485 | break; |
| 1486 | } |
| 1487 | // ELSE FALL THROUGH |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1488 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1489 | |
| 1490 | case 6: // stmt: BrCond(bool) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1491 | { // bool => boolean was computed with some boolean operator |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1492 | // (SetCC, Not, ...). We need to check whether the type was a FP, |
| 1493 | // signed int or unsigned int, and check the branching condition in |
| 1494 | // order to choose the branch to use. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1495 | // If it is an integer CC, we also need to find the unique |
| 1496 | // TmpInstruction representing that CC. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1497 | // |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1498 | BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1499 | bool isFPBranch; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1500 | M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch)); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1501 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1502 | Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(), |
| 1503 | brInst->getParent()->getParent(), |
| 1504 | isFPBranch? Type::FloatTy : Type::IntTy); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1505 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1506 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue); |
| 1507 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
| 1508 | brInst->getSuccessor(0)); |
| 1509 | mvec.push_back(M); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1510 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1511 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1512 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1513 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1514 | // false branch |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1515 | M = new MachineInstr(BA); |
| 1516 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
| 1517 | (Value*) NULL); |
| 1518 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
| 1519 | brInst->getSuccessor(1)); |
| 1520 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1521 | |
| 1522 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1523 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1524 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1525 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1526 | |
| 1527 | case 208: // stmt: BrCond(boolconst) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1528 | { |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1529 | // boolconst => boolean is a constant; use BA to first or second label |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1530 | Constant* constVal = |
| 1531 | cast<Constant>(subtreeRoot->leftChild()->getValue()); |
| 1532 | unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1533 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1534 | M = new MachineInstr(BA); |
| 1535 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
| 1536 | (Value*) NULL); |
| 1537 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1538 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1539 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1540 | |
| 1541 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1542 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1543 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1544 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1545 | |
| 1546 | case 8: // stmt: BrCond(boolreg) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1547 | { // boolreg => boolean is stored in an existing register. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1548 | // Just use the branch-on-integer-register instruction! |
| 1549 | // |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1550 | M = new MachineInstr(BRNZ); |
| 1551 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1552 | subtreeRoot->leftChild()->getValue()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1553 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1554 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1555 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1556 | |
| 1557 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1558 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1559 | |
| 1560 | // false branch |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1561 | M = new MachineInstr(BA); |
| 1562 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
| 1563 | (Value*) NULL); |
| 1564 | M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1565 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1566 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1567 | |
| 1568 | // delay slot |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1569 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1570 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1571 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1572 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1573 | case 9: // stmt: Switch(reg) |
| 1574 | assert(0 && "*** SWITCH instruction is not implemented yet."); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1575 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1576 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1577 | case 10: // reg: VRegList(reg, reg) |
| 1578 | assert(0 && "VRegList should never be the topmost non-chain rule"); |
| 1579 | break; |
| 1580 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1581 | case 21: // bool: Not(bool): Both these are implemented as: |
Vikram S. Adve | 85e1e9c | 2002-04-01 20:28:48 +0000 | [diff] [blame^] | 1582 | case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0 |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1583 | M = new MachineInstr(XNOR); |
| 1584 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 1585 | subtreeRoot->leftChild()->getValue()); |
| 1586 | M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum()); |
| 1587 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, |
| 1588 | subtreeRoot->getValue()); |
| 1589 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1590 | break; |
| 1591 | |
| 1592 | case 322: // reg: ToBoolTy(bool): |
| 1593 | case 22: // reg: ToBoolTy(reg): |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1594 | { |
| 1595 | const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 1596 | assert(opType->isIntegral() || opType->isPointerType() |
| 1597 | || opType == Type::BoolTy); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1598 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1599 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1602 | case 23: // reg: ToUByteTy(reg) |
| 1603 | case 25: // reg: ToUShortTy(reg) |
| 1604 | case 27: // reg: ToUIntTy(reg) |
| 1605 | case 29: // reg: ToULongTy(reg) |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1606 | { |
| 1607 | const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1608 | assert(opType->isIntegral() || |
| 1609 | opType->isPointerType() || |
| 1610 | opType == Type::BoolTy && "Cast is illegal for other types"); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1611 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1612 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1615 | case 24: // reg: ToSByteTy(reg) |
| 1616 | case 26: // reg: ToShortTy(reg) |
| 1617 | case 28: // reg: ToIntTy(reg) |
| 1618 | case 30: // reg: ToLongTy(reg) |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1619 | { |
| 1620 | const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 1621 | if (opType->isIntegral() |
| 1622 | || opType->isPointerType() |
| 1623 | || opType == Type::BoolTy) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1624 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1625 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1626 | } |
| 1627 | else |
| 1628 | { |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1629 | // If the source operand is an FP type, the int result must be |
| 1630 | // copied from float to int register via memory! |
| 1631 | Instruction *dest = subtreeRoot->getInstruction(); |
| 1632 | Value* leftVal = subtreeRoot->leftChild()->getValue(); |
| 1633 | Value* destForCast; |
| 1634 | vector<MachineInstr*> minstrVec; |
| 1635 | |
| 1636 | if (opType == Type::FloatTy || opType == Type::DoubleTy) |
| 1637 | { |
| 1638 | // Create a temporary to represent the INT register |
| 1639 | // into which the FP value will be copied via memory. |
| 1640 | // The type of this temporary will determine the FP |
| 1641 | // register used: single-prec for a 32-bit int or smaller, |
| 1642 | // double-prec for a 64-bit int. |
| 1643 | // |
| 1644 | const Type* destTypeToUse = |
| 1645 | (dest->getType() == Type::LongTy)? Type::DoubleTy |
| 1646 | : Type::FloatTy; |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1647 | destForCast = new TmpInstruction(destTypeToUse, leftVal); |
| 1648 | MachineCodeForInstruction &MCFI = |
| 1649 | MachineCodeForInstruction::get(dest); |
| 1650 | MCFI.addTemp(destForCast); |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1651 | |
| 1652 | vector<TmpInstruction*> tempVec; |
| 1653 | target.getInstrInfo().CreateCodeToCopyFloatToInt( |
| 1654 | dest->getParent()->getParent(), |
| 1655 | (TmpInstruction*) destForCast, dest, |
| 1656 | minstrVec, tempVec, target); |
| 1657 | |
| 1658 | for (unsigned i=0; i < tempVec.size(); ++i) |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1659 | MCFI.addTemp(tempVec[i]); |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1660 | } |
| 1661 | else |
| 1662 | destForCast = leftVal; |
| 1663 | |
| 1664 | MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType); |
| 1665 | assert(opCode != INVALID_OPCODE && "Expected to need conversion!"); |
| 1666 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1667 | M = new MachineInstr(opCode); |
| 1668 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 1669 | leftVal); |
| 1670 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, |
| 1671 | destForCast); |
| 1672 | mvec.push_back(M); |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1673 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1674 | // Append the copy code, if any, after the conversion instr. |
| 1675 | mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1676 | } |
| 1677 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1680 | case 31: // reg: ToFloatTy(reg): |
| 1681 | case 32: // reg: ToDoubleTy(reg): |
| 1682 | case 232: // reg: ToDoubleTy(Constant): |
| 1683 | |
| 1684 | // If this instruction has a parent (a user) in the tree |
| 1685 | // and the user is translated as an FsMULd instruction, |
| 1686 | // then the cast is unnecessary. So check that first. |
| 1687 | // In the future, we'll want to do the same for the FdMULq instruction, |
| 1688 | // so do the check here instead of only for ToFloatTy(reg). |
| 1689 | // |
| 1690 | if (subtreeRoot->parent() != NULL && |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1691 | MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1692 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1693 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1694 | } |
| 1695 | else |
| 1696 | { |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1697 | Value* leftVal = subtreeRoot->leftChild()->getValue(); |
| 1698 | const Type* opType = leftVal->getType(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1699 | MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType); |
| 1700 | if (opCode == INVALID_OPCODE) // no conversion needed |
| 1701 | { |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1702 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1703 | } |
| 1704 | else |
| 1705 | { |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1706 | // If the source operand is a non-FP type it must be |
| 1707 | // first copied from int to float register via memory! |
| 1708 | Instruction *dest = subtreeRoot->getInstruction(); |
| 1709 | Value* srcForCast; |
| 1710 | int n = 0; |
| 1711 | if (opType != Type::FloatTy && opType != Type::DoubleTy) |
| 1712 | { |
| 1713 | // Create a temporary to represent the FP register |
| 1714 | // into which the integer will be copied via memory. |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1715 | // The type of this temporary will determine the FP |
| 1716 | // register used: single-prec for a 32-bit int or smaller, |
| 1717 | // double-prec for a 64-bit int. |
| 1718 | // |
| 1719 | const Type* srcTypeToUse = |
| 1720 | (leftVal->getType() == Type::LongTy)? Type::DoubleTy |
| 1721 | : Type::FloatTy; |
| 1722 | |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1723 | srcForCast = new TmpInstruction(srcTypeToUse, dest); |
| 1724 | MachineCodeForInstruction &DestMCFI = |
| 1725 | MachineCodeForInstruction::get(dest); |
| 1726 | DestMCFI.addTemp(srcForCast); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1727 | |
| 1728 | vector<MachineInstr*> minstrVec; |
| 1729 | vector<TmpInstruction*> tempVec; |
| 1730 | target.getInstrInfo().CreateCodeToCopyIntToFloat( |
| 1731 | dest->getParent()->getParent(), |
| 1732 | leftVal, (TmpInstruction*) srcForCast, |
| 1733 | minstrVec, tempVec, target); |
| 1734 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1735 | mvec.insert(mvec.end(), minstrVec.begin(),minstrVec.end()); |
| 1736 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1737 | for (unsigned i=0; i < tempVec.size(); ++i) |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1738 | DestMCFI.addTemp(tempVec[i]); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1739 | } |
| 1740 | else |
| 1741 | srcForCast = leftVal; |
| 1742 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1743 | M = new MachineInstr(opCode); |
| 1744 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 1745 | srcForCast); |
| 1746 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, |
| 1747 | dest); |
| 1748 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | break; |
| 1752 | |
| 1753 | case 19: // reg: ToArrayTy(reg): |
| 1754 | case 20: // reg: ToPointerTy(reg): |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1755 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1756 | break; |
| 1757 | |
| 1758 | case 233: // reg: Add(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1759 | M = CreateAddConstInstruction(subtreeRoot); |
| 1760 | if (M != NULL) |
| 1761 | { |
| 1762 | mvec.push_back(M); |
| 1763 | break; |
| 1764 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1765 | // ELSE FALL THROUGH |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1766 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1767 | case 33: // reg: Add(reg, reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1768 | mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot))); |
| 1769 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1770 | break; |
| 1771 | |
| 1772 | case 234: // reg: Sub(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1773 | M = CreateSubConstInstruction(subtreeRoot); |
| 1774 | if (M != NULL) |
| 1775 | { |
| 1776 | mvec.push_back(M); |
| 1777 | break; |
| 1778 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1779 | // ELSE FALL THROUGH |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1780 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1781 | case 34: // reg: Sub(reg, reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1782 | mvec.push_back(new MachineInstr(ChooseSubInstructionByType( |
| 1783 | subtreeRoot->getInstruction()->getType()))); |
| 1784 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1785 | break; |
| 1786 | |
| 1787 | case 135: // reg: Mul(todouble, todouble) |
| 1788 | checkCast = true; |
| 1789 | // FALL THROUGH |
| 1790 | |
| 1791 | case 35: // reg: Mul(reg, reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1792 | { |
| 1793 | MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot)) |
| 1794 | ? FSMULD |
| 1795 | : INVALID_MACHINE_OPCODE); |
| 1796 | CreateMulInstruction(target, |
| 1797 | subtreeRoot->leftChild()->getValue(), |
| 1798 | subtreeRoot->rightChild()->getValue(), |
| 1799 | subtreeRoot->getInstruction(), |
| 1800 | mvec, forceOp); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1801 | break; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1802 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1803 | case 335: // reg: Mul(todouble, todoubleConst) |
| 1804 | checkCast = true; |
| 1805 | // FALL THROUGH |
| 1806 | |
| 1807 | case 235: // reg: Mul(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1808 | { |
| 1809 | MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot)) |
| 1810 | ? FSMULD |
| 1811 | : INVALID_MACHINE_OPCODE); |
| 1812 | CreateMulInstruction(target, |
| 1813 | subtreeRoot->leftChild()->getValue(), |
| 1814 | subtreeRoot->rightChild()->getValue(), |
| 1815 | subtreeRoot->getInstruction(), |
| 1816 | mvec, forceOp); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1817 | break; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1818 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1819 | case 236: // reg: Div(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1820 | L = mvec.size(); |
| 1821 | CreateDivConstInstruction(target, subtreeRoot, mvec); |
| 1822 | if (mvec.size() > L) |
| 1823 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1824 | // ELSE FALL THROUGH |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1825 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1826 | case 36: // reg: Div(reg, reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1827 | mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot))); |
| 1828 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1829 | break; |
| 1830 | |
| 1831 | case 37: // reg: Rem(reg, reg) |
| 1832 | case 237: // reg: Rem(reg, Constant) |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1833 | { |
| 1834 | Instruction* remInstr = subtreeRoot->getInstruction(); |
| 1835 | |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1836 | TmpInstruction* quot = new TmpInstruction( |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1837 | subtreeRoot->leftChild()->getValue(), |
| 1838 | subtreeRoot->rightChild()->getValue()); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1839 | TmpInstruction* prod = new TmpInstruction( |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1840 | quot, |
| 1841 | subtreeRoot->rightChild()->getValue()); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1842 | MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1843 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1844 | M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot)); |
| 1845 | Set3OperandsFromInstr(M, subtreeRoot, target); |
| 1846 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot); |
| 1847 | mvec.push_back(M); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1848 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1849 | M = new MachineInstr(ChooseMulInstructionByType( |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1850 | subtreeRoot->getInstruction()->getType())); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1851 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot); |
| 1852 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1853 | subtreeRoot->rightChild()->getValue()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1854 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod); |
| 1855 | mvec.push_back(M); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1856 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1857 | M = new MachineInstr(ChooseSubInstructionByType( |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1858 | subtreeRoot->getInstruction()->getType())); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1859 | Set3OperandsFromInstr(M, subtreeRoot, target); |
| 1860 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod); |
| 1861 | mvec.push_back(M); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1862 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1863 | break; |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1866 | case 38: // bool: And(bool, bool) |
| 1867 | case 238: // bool: And(bool, boolconst) |
| 1868 | case 338: // reg : BAnd(reg, reg) |
| 1869 | case 538: // reg : BAnd(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1870 | mvec.push_back(new MachineInstr(AND)); |
| 1871 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1872 | break; |
| 1873 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1874 | case 138: // bool: And(bool, not) |
| 1875 | case 438: // bool: BAnd(bool, not) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1876 | mvec.push_back(new MachineInstr(ANDN)); |
| 1877 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1880 | case 39: // bool: Or(bool, bool) |
| 1881 | case 239: // bool: Or(bool, boolconst) |
| 1882 | case 339: // reg : BOr(reg, reg) |
| 1883 | case 539: // reg : BOr(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1884 | mvec.push_back(new MachineInstr(ORN)); |
| 1885 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1886 | break; |
| 1887 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1888 | case 139: // bool: Or(bool, not) |
| 1889 | case 439: // bool: BOr(bool, not) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1890 | mvec.push_back(new MachineInstr(ORN)); |
| 1891 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1892 | break; |
| 1893 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1894 | case 40: // bool: Xor(bool, bool) |
| 1895 | case 240: // bool: Xor(bool, boolconst) |
| 1896 | case 340: // reg : BXor(reg, reg) |
| 1897 | case 540: // reg : BXor(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1898 | mvec.push_back(new MachineInstr(XOR)); |
| 1899 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1900 | break; |
| 1901 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1902 | case 140: // bool: Xor(bool, not) |
| 1903 | case 440: // bool: BXor(bool, not) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1904 | mvec.push_back(new MachineInstr(XNOR)); |
| 1905 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | case 41: // boolconst: SetCC(reg, Constant) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1909 | // |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1910 | // If the SetCC was folded into the user (parent), it will be |
| 1911 | // caught above. All other cases are the same as case 42, |
| 1912 | // so just fall through. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1913 | // |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1914 | case 42: // bool: SetCC(reg, reg): |
| 1915 | { |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1916 | // This generates a SUBCC instruction, putting the difference in |
| 1917 | // a result register, and setting a condition code. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1918 | // |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1919 | // If the boolean result of the SetCC is used by anything other |
| 1920 | // than a single branch instruction, the boolean must be |
| 1921 | // computed and stored in the result register. Otherwise, discard |
| 1922 | // the difference (by using %g0) and keep only the condition code. |
| 1923 | // |
| 1924 | // To compute the boolean result in a register we use a conditional |
| 1925 | // move, unless the result of the SUBCC instruction can be used as |
| 1926 | // the bool! This assumes that zero is FALSE and any non-zero |
| 1927 | // integer is TRUE. |
| 1928 | // |
| 1929 | InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent(); |
| 1930 | Instruction* setCCInstr = subtreeRoot->getInstruction(); |
| 1931 | bool keepBoolVal = (parentNode == NULL || |
| 1932 | parentNode->getInstruction()->getOpcode() |
| 1933 | != Instruction::Br); |
| 1934 | bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1935 | bool keepSubVal = keepBoolVal && subValIsBoolVal; |
| 1936 | bool computeBoolVal = keepBoolVal && ! subValIsBoolVal; |
| 1937 | |
| 1938 | bool mustClearReg; |
| 1939 | int valueToMove; |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 1940 | MachineOpCode movOpCode = 0; |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1941 | |
| 1942 | // Mark the 4th operand as being a CC register, and as a def |
| 1943 | // A TmpInstruction is created to represent the CC "result". |
| 1944 | // Unlike other instances of TmpInstruction, this one is used |
| 1945 | // by machine code of multiple LLVM instructions, viz., |
| 1946 | // the SetCC and the branch. Make sure to get the same one! |
| 1947 | // Note that we do this even for FP CC registers even though they |
| 1948 | // are explicit operands, because the type of the operand |
| 1949 | // needs to be a floating point condition code, not an integer |
| 1950 | // condition code. Think of this as casting the bool result to |
| 1951 | // a FP condition code register. |
| 1952 | // |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1953 | Value* leftVal = subtreeRoot->leftChild()->getValue(); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1954 | bool isFPCompare = (leftVal->getType() == Type::FloatTy || |
| 1955 | leftVal->getType() == Type::DoubleTy); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1956 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1957 | TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr, |
| 1958 | setCCInstr->getParent()->getParent(), |
| 1959 | isFPCompare? Type::FloatTy : Type::IntTy); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1960 | MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1961 | |
| 1962 | if (! isFPCompare) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1963 | { |
| 1964 | // Integer condition: dest. should be %g0 or an integer register. |
| 1965 | // If result must be saved but condition is not SetEQ then we need |
| 1966 | // a separate instruction to compute the bool result, so discard |
| 1967 | // result of SUBcc instruction anyway. |
| 1968 | // |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1969 | M = new MachineInstr(SUBcc); |
| 1970 | Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal); |
| 1971 | M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister, |
| 1972 | tmpForCC, /*def*/true); |
| 1973 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1974 | |
| 1975 | if (computeBoolVal) |
| 1976 | { // recompute bool using the integer condition codes |
| 1977 | movOpCode = |
| 1978 | ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove); |
| 1979 | } |
| 1980 | } |
| 1981 | else |
| 1982 | { |
| 1983 | // FP condition: dest of FCMP should be some FCCn register |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1984 | M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot)); |
| 1985 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1986 | tmpForCC); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1987 | M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1988 | subtreeRoot->leftChild()->getValue()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1989 | M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1990 | subtreeRoot->rightChild()->getValue()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1991 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1992 | |
| 1993 | if (computeBoolVal) |
| 1994 | {// recompute bool using the FP condition codes |
| 1995 | mustClearReg = true; |
| 1996 | valueToMove = 1; |
| 1997 | movOpCode = ChooseMovFpccInstruction(subtreeRoot); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | if (computeBoolVal) |
| 2002 | { |
| 2003 | if (mustClearReg) |
| 2004 | {// Unconditionally set register to 0 |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2005 | M = new MachineInstr(SETHI); |
| 2006 | M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed, |
| 2007 | (int64_t)0); |
| 2008 | M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, |
| 2009 | setCCInstr); |
| 2010 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | // Now conditionally move `valueToMove' (0 or 1) into the register |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2014 | M = new MachineInstr(movOpCode); |
| 2015 | M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, |
| 2016 | tmpForCC); |
| 2017 | M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed, |
| 2018 | valueToMove); |
| 2019 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, |
| 2020 | setCCInstr); |
| 2021 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2022 | } |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | case 43: // boolreg: VReg |
| 2027 | case 44: // boolreg: Constant |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2028 | break; |
| 2029 | |
| 2030 | case 51: // reg: Load(reg) |
| 2031 | case 52: // reg: Load(ptrreg) |
| 2032 | case 53: // reg: LoadIdx(reg,reg) |
| 2033 | case 54: // reg: LoadIdx(ptrreg,reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2034 | mvec.push_back(new MachineInstr(ChooseLoadInstruction( |
| 2035 | subtreeRoot->getValue()->getType()))); |
| 2036 | SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | |
| 2039 | case 55: // reg: GetElemPtr(reg) |
| 2040 | case 56: // reg: GetElemPtrIdx(reg,reg) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2041 | // If the GetElemPtr was folded into the user (parent), it will be |
| 2042 | // caught above. For other cases, we have to compute the address. |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2043 | mvec.push_back(new MachineInstr(ADD)); |
| 2044 | SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2045 | break; |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2046 | |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2047 | case 57: // reg: Alloca: Implement as 1 instruction: |
| 2048 | { // add %fp, offsetFromFP -> result |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2049 | AllocationInst* instr = |
| 2050 | cast<AllocationInst>(subtreeRoot->getInstruction()); |
| 2051 | unsigned int tsize = |
| 2052 | target.findOptimalStorageSize(instr->getAllocatedType()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2053 | assert(tsize != 0); |
| 2054 | CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2055 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2056 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2057 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2058 | case 58: // reg: Alloca(reg): Implement as 3 instructions: |
| 2059 | // mul num, typeSz -> tmp |
| 2060 | // sub %sp, tmp -> %sp |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2061 | { // add %sp, frameSizeBelowDynamicArea -> result |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2062 | AllocationInst* instr = |
| 2063 | cast<AllocationInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2064 | const Type* eltType = instr->getAllocatedType(); |
| 2065 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2066 | // If #elements is constant, use simpler code for fixed-size allocas |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2067 | int tsize = (int) target.findOptimalStorageSize(eltType); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2068 | Value* numElementsVal = NULL; |
| 2069 | bool isArray = instr->isArrayAllocation(); |
| 2070 | |
| 2071 | if (!isArray || |
| 2072 | isa<Constant>(numElementsVal = instr->getArraySize())) |
| 2073 | { // total size is constant: generate code for fixed-size alloca |
| 2074 | unsigned int numElements = isArray? |
| 2075 | cast<ConstantUInt>(numElementsVal)->getValue() : 1; |
| 2076 | CreateCodeForFixedSizeAlloca(target, instr, tsize, |
| 2077 | numElements, mvec); |
| 2078 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2079 | else // total size is not constant. |
| 2080 | CreateCodeForVariableSizeAlloca(target, instr, tsize, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2081 | numElementsVal, mvec); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2082 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2083 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2084 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2085 | case 61: // reg: Call |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2086 | { // Generate a call-indirect (i.e., jmpl) for now to expose |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2087 | // the potential need for registers. If an absolute address |
| 2088 | // is available, replace this with a CALL instruction. |
| 2089 | // Mark both the indirection register and the return-address |
| 2090 | // register as hidden virtual registers. |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 2091 | // Also, mark the operands of the Call and return value (if |
| 2092 | // any) as implicit operands of the CALL machine instruction. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2093 | // |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 2094 | CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction()); |
Chris Lattner | 749655f | 2001-10-13 06:54:30 +0000 | [diff] [blame] | 2095 | Value *callee = callInstr->getCalledValue(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2096 | |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 2097 | // Create hidden virtual register for return address, with type void*. |
| 2098 | Instruction* retAddrReg = |
| 2099 | new TmpInstruction(PointerType::get(Type::VoidTy), callInstr); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 2100 | MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2101 | |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2102 | // Generate the machine instruction and its operands. |
| 2103 | // Use CALL for direct function calls; this optimistically assumes |
| 2104 | // the PC-relative address fits in the CALL address field (22 bits). |
| 2105 | // Use JMPL for indirect calls. |
| 2106 | // |
Chris Lattner | b0d0472 | 2002-03-26 17:58:12 +0000 | [diff] [blame] | 2107 | if (isa<Function>(callee)) |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2108 | { // direct function call |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2109 | M = new MachineInstr(CALL); |
| 2110 | M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp, |
| 2111 | callee); |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2112 | } |
| 2113 | else |
| 2114 | { // indirect function call |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2115 | M = new MachineInstr(JMPLCALL); |
| 2116 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
| 2117 | callee); |
| 2118 | M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, |
| 2119 | (int64_t) 0); |
| 2120 | M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, |
| 2121 | retAddrReg); |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2122 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2123 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2124 | mvec.push_back(M); |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 2125 | |
| 2126 | // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses. |
| 2127 | // The result value must go in slot N. This is assumed |
| 2128 | // in register allocation. |
| 2129 | // |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 2130 | // Add the call operands and return value as implicit refs |
| 2131 | for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i) |
| 2132 | if (callInstr->getOperand(i) != callee) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2133 | mvec.back()->addImplicitRef(callInstr->getOperand(i)); |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 2134 | |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2135 | if (callInstr->getType() != Type::VoidTy) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2136 | mvec.back()->addImplicitRef(callInstr, /*isDef*/ true); |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 2137 | |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2138 | // For the CALL instruction, the ret. addr. reg. is also implicit |
Chris Lattner | b0d0472 | 2002-03-26 17:58:12 +0000 | [diff] [blame] | 2139 | if (isa<Function>(callee)) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2140 | mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2141 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2142 | // delay slot |
| 2143 | mvec.push_back(new MachineInstr(NOP)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2144 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2145 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2146 | |
| 2147 | case 62: // reg: Shl(reg, reg) |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2148 | { const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2149 | assert(opType->isIntegral() |
| 2150 | || opType == Type::BoolTy |
| 2151 | || opType->isPointerType()&& "Shl unsupported for other types"); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2152 | mvec.push_back(new MachineInstr((opType == Type::LongTy)? SLLX : SLL)); |
| 2153 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2154 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2157 | case 63: // reg: Shr(reg, reg) |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2158 | { const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2159 | assert(opType->isIntegral() |
| 2160 | || opType == Type::BoolTy |
| 2161 | || opType->isPointerType() &&"Shr unsupported for other types"); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2162 | mvec.push_back(new MachineInstr((opType->isSigned() |
| 2163 | ? ((opType == Type::LongTy)? SRAX : SRA) |
| 2164 | : ((opType == Type::LongTy)? SRLX : SRL)))); |
| 2165 | Set3OperandsFromInstr(mvec.back(), subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2166 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2169 | case 64: // reg: Phi(reg,reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2170 | break; // don't forward the value |
| 2171 | |
Vikram S. Adve | 3438b21 | 2001-11-12 18:54:11 +0000 | [diff] [blame] | 2172 | #undef NEED_PHI_MACHINE_INSTRS |
| 2173 | #ifdef NEED_PHI_MACHINE_INSTRS |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2174 | { // This instruction has variable #operands, so resultPos is 0. |
| 2175 | Instruction* phi = subtreeRoot->getInstruction(); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2176 | M = new MachineInstr(PHI, 1 + phi->getNumOperands()); |
| 2177 | M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2178 | subtreeRoot->getValue()); |
| 2179 | for (unsigned i=0, N=phi->getNumOperands(); i < N; i++) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2180 | M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister, |
| 2181 | phi->getOperand(i)); |
| 2182 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2183 | break; |
| 2184 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 2185 | #endif // NEED_PHI_MACHINE_INSTRS |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2186 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2187 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2188 | case 71: // reg: VReg |
| 2189 | case 72: // reg: Constant |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2190 | break; // don't forward the value |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2191 | |
| 2192 | default: |
| 2193 | assert(0 && "Unrecognized BURG rule"); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2194 | break; |
| 2195 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2196 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2197 | |
| 2198 | if (forwardOperandNum >= 0) |
| 2199 | { // We did not generate a machine instruction but need to use operand. |
| 2200 | // If user is in the same tree, replace Value in its machine operand. |
| 2201 | // If not, insert a copy instruction which should get coalesced away |
| 2202 | // by register allocation. |
| 2203 | if (subtreeRoot->parent() != NULL) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2204 | ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2205 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2206 | { |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 2207 | vector<MachineInstr*> minstrVec; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2208 | target.getInstrInfo().CreateCopyInstructionsByType(target, |
| 2209 | subtreeRoot->getInstruction()->getParent()->getParent(), |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2210 | subtreeRoot->getInstruction()->getOperand(forwardOperandNum), |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 2211 | subtreeRoot->getInstruction(), minstrVec); |
| 2212 | assert(minstrVec.size() > 0); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2213 | mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2214 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2215 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | |