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