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