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