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