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