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