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