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