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