Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1 | // $Id$ |
| 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // SparcInstrSelection.cpp |
| 5 | // |
| 6 | // Purpose: |
| 7 | // |
| 8 | // History: |
| 9 | // 7/02/01 - Vikram Adve - Created |
| 10 | //*************************************************************************** |
| 11 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 12 | #include "llvm/Type.h" |
| 13 | #include "llvm/DerivedTypes.h" |
| 14 | #include "llvm/SymbolTable.h" |
| 15 | #include "llvm/Value.h" |
| 16 | #include "llvm/Instruction.h" |
| 17 | #include "llvm/InstrTypes.h" |
| 18 | #include "llvm/iTerminators.h" |
| 19 | #include "llvm/iMemory.h" |
| 20 | #include "llvm/iOther.h" |
| 21 | #include "llvm/BasicBlock.h" |
| 22 | #include "llvm/Method.h" |
| 23 | #include "llvm/ConstPoolVals.h" |
Chris Lattner | 7e583cf | 2001-07-21 20:58:30 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Sparc.h" |
| 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/InstrForest.h" |
| 27 | #include "llvm/CodeGen/InstrSelection.h" |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | //******************** Internal Data Declarations ************************/ |
| 31 | |
| 32 | // to be used later |
| 33 | struct BranchPattern { |
| 34 | bool flipCondition; // should the sense of the test be reversed |
| 35 | BasicBlock* targetBB; // which basic block to branch to |
| 36 | MachineInstr* extraBranch; // if neither branch is fall-through, then this |
| 37 | // BA must be inserted after the cond'l one |
| 38 | }; |
| 39 | |
| 40 | //************************* Forward Declarations ***************************/ |
| 41 | |
| 42 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 43 | static MachineOpCode ChooseBprInstruction (const InstructionNode* instrNode); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 44 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 45 | static MachineOpCode ChooseBccInstruction (const InstructionNode* instrNode, |
| 46 | bool& isFPBranch); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 47 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 48 | static MachineOpCode ChooseBpccInstruction (const InstructionNode* instrNode, |
| 49 | const BinaryOperator* setCCInst); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 50 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 51 | static MachineOpCode ChooseBFpccInstruction (const InstructionNode* instrNode, |
| 52 | const BinaryOperator* setCCInst); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 53 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 54 | static MachineOpCode ChooseMovFpccInstruction(const InstructionNode*); |
| 55 | |
| 56 | static MachineOpCode ChooseMovpccAfterSub (const InstructionNode* instrNode, |
| 57 | bool& mustClearReg, |
| 58 | int& valueToMove); |
| 59 | |
| 60 | static MachineOpCode ChooseConvertToFloatInstr(const InstructionNode*, |
Chris Lattner | 51a9ad9 | 2001-07-21 22:57:05 +0000 | [diff] [blame] | 61 | const Type* opType); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 62 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 63 | static MachineOpCode ChooseConvertToIntInstr(const InstructionNode* instrNode, |
| 64 | const Type* opType); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 65 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 66 | static MachineOpCode ChooseAddInstruction (const InstructionNode* instrNode); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 67 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 68 | static MachineOpCode ChooseSubInstruction (const InstructionNode* instrNode); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 69 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 70 | static MachineOpCode ChooseFcmpInstruction (const InstructionNode* instrNode); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 71 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 72 | static MachineOpCode ChooseMulInstruction (const InstructionNode* instrNode, |
| 73 | bool checkCasts); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 74 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 75 | static MachineOpCode ChooseDivInstruction (const InstructionNode* instrNode); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 76 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 77 | static MachineOpCode ChooseLoadInstruction (const Type* resultType); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 78 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 79 | static MachineOpCode ChooseStoreInstruction (const Type* valueType); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 80 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 81 | static void SetOperandsForMemInstr(MachineInstr* minstr, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 82 | const InstructionNode* vmInstrNode, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 83 | const TargetMachine& target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 84 | |
| 85 | static void SetMemOperands_Internal (MachineInstr* minstr, |
| 86 | const InstructionNode* vmInstrNode, |
| 87 | Value* ptrVal, |
| 88 | Value* arrayOffsetVal, |
| 89 | const vector<ConstPoolVal*>& idxVec, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 90 | const TargetMachine& target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 91 | |
| 92 | static unsigned FixConstantOperands(const InstructionNode* vmInstrNode, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 93 | MachineInstr** mvec, |
| 94 | unsigned numInstr, |
| 95 | TargetMachine& target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 96 | |
| 97 | static unsigned InsertLoadConstInstructions(unsigned loadConstFlags, |
| 98 | const InstructionNode* vmInstrNode, |
| 99 | MachineInstr** mvec, |
| 100 | unsigned numInstr); |
| 101 | |
| 102 | static MachineInstr* MakeOneLoadConstInstr(Instruction* vmInstr, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 103 | Value* val, |
| 104 | TmpInstruction*& tmpReg); |
| 105 | |
| 106 | static void ForwardOperand (InstructionNode* treeNode, |
| 107 | InstructionNode* parent, |
| 108 | int operandNum); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | //******************* Externally Visible Functions *************************/ |
| 112 | |
| 113 | |
| 114 | //------------------------------------------------------------------------ |
| 115 | // External Function: ThisIsAChainRule |
| 116 | // |
| 117 | // Purpose: |
| 118 | // Check if a given BURG rule is a chain rule. |
| 119 | //------------------------------------------------------------------------ |
| 120 | |
| 121 | extern bool |
| 122 | ThisIsAChainRule(int eruleno) |
| 123 | { |
| 124 | switch(eruleno) |
| 125 | { |
| 126 | case 111: // stmt: reg |
| 127 | case 112: // stmt: boolconst |
| 128 | case 113: // stmt: bool |
| 129 | case 121: |
| 130 | case 122: |
| 131 | case 123: |
| 132 | case 124: |
| 133 | case 125: |
| 134 | case 126: |
| 135 | case 127: |
| 136 | case 128: |
| 137 | case 129: |
| 138 | case 130: |
| 139 | case 131: |
| 140 | case 132: |
Vikram S. Adve | 74f4a13 | 2001-07-31 21:46:57 +0000 | [diff] [blame^] | 141 | case 153: |
| 142 | case 155: return true; break; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 143 | |
| 144 | default: return false; break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | //------------------------------------------------------------------------ |
| 149 | // External Function: GetInstructionsByRule |
| 150 | // |
| 151 | // Purpose: |
| 152 | // Choose machine instructions for the SPARC according to the |
| 153 | // patterns chosen by the BURG-generated parser. |
| 154 | //------------------------------------------------------------------------ |
| 155 | |
| 156 | unsigned |
| 157 | GetInstructionsByRule(InstructionNode* subtreeRoot, |
| 158 | int ruleForNode, |
| 159 | short* nts, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 160 | TargetMachine &target, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 161 | MachineInstr** mvec) |
| 162 | { |
| 163 | int numInstr = 1; // initialize for common case |
| 164 | bool checkCast = false; // initialize here to use fall-through |
| 165 | Value *leftVal, *rightVal; |
| 166 | const Type* opType; |
| 167 | int nextRule; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 168 | int forwardOperandNum = -1; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 169 | BranchPattern brPattern; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 170 | int64_t s0 = 0; // variables holding zero to avoid |
| 171 | uint64_t u0 = 0; // overloading ambiguities below |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 172 | |
| 173 | mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety |
| 174 | |
| 175 | switch(ruleForNode) { |
| 176 | case 1: // stmt: Ret |
| 177 | case 2: // stmt: RetValue(reg) |
| 178 | // NOTE: Prepass of register allocation is responsible |
| 179 | // for moving return value to appropriate register. |
| 180 | // Mark the return-address register as a hidden virtual reg. |
| 181 | { |
| 182 | Instruction* returnReg = new TmpInstruction(Instruction::UserOp1, |
| 183 | subtreeRoot->getInstruction(), NULL); |
| 184 | subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg); |
| 185 | |
| 186 | mvec[0] = new MachineInstr(RETURN); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 187 | mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg); |
| 188 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0); |
| 189 | |
| 190 | returnReg->addMachineInstruction(mvec[0]); |
| 191 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 192 | mvec[numInstr++] = new MachineInstr(NOP); // delay slot |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | case 3: // stmt: Store(reg,reg) |
| 197 | case 4: // stmt: Store(reg,ptrreg) |
| 198 | mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType())); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 199 | SetOperandsForMemInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case 5: // stmt: BrUncond |
| 203 | mvec[0] = new MachineInstr(BA); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 204 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL); |
| 205 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 206 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0)); |
| 207 | |
| 208 | mvec[numInstr++] = new MachineInstr(NOP); // delay slot |
| 209 | break; |
| 210 | |
| 211 | case 6: // stmt: BrCond(boolconst) |
| 212 | // boolconst => boolean was computed with `%b = setCC type reg1 constant' |
| 213 | // If the constant is ZERO, we can use the branch-on-integer-register |
| 214 | // instructions and avoid the SUBcc instruction entirely. |
| 215 | // Otherwise this is just the same as case 5, so just fall through. |
| 216 | { |
| 217 | InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild(); |
| 218 | assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode); |
| 219 | ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue(); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 220 | bool isValidConst; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 221 | |
| 222 | if (constVal->getType()->isIntegral() |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 223 | && GetConstantValueAsSignedInt(constVal, isValidConst) == 0 |
| 224 | && isValidConst) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 225 | { |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 226 | // That constant ia a zero after all... |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 227 | // Use the left child of the setCC instruction as the first argument! |
| 228 | mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 229 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 230 | subtreeRoot->leftChild()->leftChild()->getValue()); |
| 231 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, |
| 232 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0)); |
| 233 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 234 | // delay slot |
| 235 | mvec[numInstr++] = new MachineInstr(NOP); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 236 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 237 | // false branch |
| 238 | mvec[numInstr++] = new MachineInstr(BA); |
| 239 | mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister, |
| 240 | (Value*) NULL); |
| 241 | mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1)); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | // ELSE FALL THROUGH |
| 245 | } |
| 246 | |
| 247 | case 7: // stmt: BrCond(bool) |
| 248 | // bool => boolean was computed with `%b = setcc type reg1 reg2' |
| 249 | // Need to check whether the type was a FP, signed int or unsigned int, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 250 | // and check the branching condition in order to choose the branch to use. |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 251 | // |
| 252 | { |
| 253 | bool isFPBranch; |
| 254 | mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 255 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, |
| 256 | subtreeRoot->leftChild()->getValue()); |
| 257 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 258 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0)); |
| 259 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 260 | // delay slot |
| 261 | mvec[numInstr++] = new MachineInstr(NOP); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 262 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 263 | // false branch |
| 264 | mvec[numInstr++] = new MachineInstr(BA); |
| 265 | mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister, |
| 266 | (Value*) NULL); |
| 267 | mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp, |
| 268 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1)); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 269 | break; |
| 270 | } |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 271 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 272 | case 8: // stmt: BrCond(boolreg) |
| 273 | // bool => boolean is stored in an existing register. |
| 274 | // Just use the branch-on-integer-register instruction! |
| 275 | // |
| 276 | mvec[0] = new MachineInstr(BRNZ); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 277 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, |
| 278 | subtreeRoot->leftChild()->getValue()); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 279 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, |
| 280 | ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0)); |
| 281 | mvec[numInstr++] = new MachineInstr(NOP); // delay slot |
| 282 | break; |
| 283 | |
| 284 | case 9: // stmt: Switch(reg) |
| 285 | assert(0 && "*** SWITCH instruction is not implemented yet."); |
| 286 | numInstr = 0; |
| 287 | break; |
| 288 | |
| 289 | case 10: // reg: VRegList(reg, reg) |
| 290 | assert(0 && "VRegList should never be the topmost non-chain rule"); |
| 291 | break; |
| 292 | |
| 293 | case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0 |
| 294 | mvec[0] = new MachineInstr(XNOR); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 295 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 296 | subtreeRoot->leftChild()->getValue()); |
| 297 | mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 298 | mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 299 | subtreeRoot->getValue()); |
| 300 | break; |
| 301 | |
| 302 | case 22: // reg: ToBoolTy(reg): |
| 303 | opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 304 | assert(opType->isIntegral() || opType == Type::BoolTy); |
| 305 | numInstr = 0; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 306 | forwardOperandNum = 0; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 307 | break; |
| 308 | |
| 309 | case 23: // reg: ToUByteTy(reg) |
| 310 | case 25: // reg: ToUShortTy(reg) |
| 311 | case 27: // reg: ToUIntTy(reg) |
| 312 | case 29: // reg: ToULongTy(reg) |
| 313 | opType = subtreeRoot->leftChild()->getValue()->getType(); |
Vikram S. Adve | 74f4a13 | 2001-07-31 21:46:57 +0000 | [diff] [blame^] | 314 | assert(opType->isIntegral() || |
| 315 | opType->isPointerType() || |
| 316 | opType == Type::BoolTy && "Ignoring cast: illegal for other types"); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 317 | numInstr = 0; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 318 | forwardOperandNum = 0; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 319 | break; |
| 320 | |
| 321 | case 24: // reg: ToSByteTy(reg) |
| 322 | case 26: // reg: ToShortTy(reg) |
| 323 | case 28: // reg: ToIntTy(reg) |
| 324 | case 30: // reg: ToLongTy(reg) |
| 325 | opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 326 | if (opType->isIntegral() || opType == Type::BoolTy) |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 327 | { |
| 328 | numInstr = 0; |
| 329 | forwardOperandNum = 0; |
| 330 | } |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 331 | else |
| 332 | { |
| 333 | mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 334 | Set2OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 335 | } |
| 336 | break; |
| 337 | |
| 338 | case 31: // reg: ToFloatTy(reg): |
| 339 | case 32: // reg: ToDoubleTy(reg): |
| 340 | |
| 341 | // If this instruction has a parent (a user) in the tree |
| 342 | // and the user is translated as an FsMULd instruction, |
| 343 | // then the cast is unnecessary. So check that first. |
| 344 | // In the future, we'll want to do the same for the FdMULq instruction, |
| 345 | // so do the check here instead of only for ToFloatTy(reg). |
| 346 | // |
| 347 | if (subtreeRoot->parent() != NULL && |
| 348 | ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD) |
| 349 | { |
| 350 | numInstr = 0; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 351 | forwardOperandNum = 0; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | { |
| 355 | opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 356 | mvec[0] = new MachineInstr(ChooseConvertToFloatInstr(subtreeRoot, opType)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 357 | Set2OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 358 | } |
| 359 | break; |
| 360 | |
| 361 | case 19: // reg: ToArrayTy(reg): |
| 362 | case 20: // reg: ToPointerTy(reg): |
| 363 | numInstr = 0; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 364 | forwardOperandNum = 0; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 365 | break; |
| 366 | |
| 367 | case 33: // reg: Add(reg, reg) |
| 368 | mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 369 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 370 | break; |
| 371 | |
| 372 | case 34: // reg: Sub(reg, reg) |
| 373 | mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 374 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 375 | break; |
| 376 | |
| 377 | case 135: // reg: Mul(todouble, todouble) |
| 378 | checkCast = true; |
| 379 | // FALL THROUGH |
| 380 | |
| 381 | case 35: // reg: Mul(reg, reg) |
| 382 | mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 383 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 384 | break; |
| 385 | |
| 386 | case 36: // reg: Div(reg, reg) |
| 387 | mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot)); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 388 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 389 | break; |
| 390 | |
| 391 | case 37: // reg: Rem(reg, reg) |
| 392 | assert(0 && "REM instruction unimplemented for the SPARC."); |
| 393 | break; |
| 394 | |
| 395 | case 38: // reg: And(reg, reg) |
| 396 | mvec[0] = new MachineInstr(AND); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 397 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 398 | break; |
| 399 | |
| 400 | case 138: // reg: And(reg, not) |
| 401 | mvec[0] = new MachineInstr(ANDN); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 402 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | case 39: // reg: Or(reg, reg) |
| 406 | mvec[0] = new MachineInstr(ORN); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 407 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case 139: // reg: Or(reg, not) |
| 411 | mvec[0] = new MachineInstr(ORN); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 412 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 413 | break; |
| 414 | |
| 415 | case 40: // reg: Xor(reg, reg) |
| 416 | mvec[0] = new MachineInstr(XOR); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 417 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 418 | break; |
| 419 | |
| 420 | case 140: // reg: Xor(reg, not) |
| 421 | mvec[0] = new MachineInstr(XNOR); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 422 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case 41: // boolconst: SetCC(reg, Constant) |
| 426 | // Check if this is an integer comparison, and |
| 427 | // there is a parent, and the parent decided to use |
| 428 | // a branch-on-integer-register instead of branch-on-condition-code. |
| 429 | // If so, the SUBcc instruction is not required. |
| 430 | // (However, we must still check for constants to be loaded from |
| 431 | // the constant pool so that such a load can be associated with |
| 432 | // this instruction.) |
| 433 | // |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 434 | // Otherwise this is just the same as case 42, so just fall through. |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 435 | // |
| 436 | if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() && |
| 437 | subtreeRoot->parent() != NULL) |
| 438 | { |
| 439 | InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent(); |
| 440 | assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
| 441 | const vector<MachineInstr*>& |
| 442 | minstrVec = parentNode->getInstruction()->getMachineInstrVec(); |
| 443 | MachineOpCode parentOpCode; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 444 | if (parentNode->getInstruction()->getOpcode() == Instruction::Br && |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 445 | (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ && |
| 446 | parentOpCode <= BRGEZ) |
| 447 | { |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 448 | numInstr = 0; // don't forward the operand! |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | } |
| 452 | // ELSE FALL THROUGH |
| 453 | |
| 454 | case 42: // bool: SetCC(reg, reg): |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 455 | { |
| 456 | // If result of the SetCC is only used for a branch, we can |
| 457 | // discard the result. otherwise, it must go into an integer register. |
| 458 | // Note that the user may or may not be in the same tree, so we have |
| 459 | // to follow SSA def-use edges here, not BURG tree edges. |
| 460 | // |
| 461 | Instruction* result = subtreeRoot->getInstruction(); |
| 462 | Value* firstUse = (Value*) * result->use_begin(); |
| 463 | bool discardResult = |
| 464 | (result->use_size() == 1 |
| 465 | && firstUse->getValueType() == Value::InstructionVal |
| 466 | && ((Instruction*) firstUse)->getOpcode() == Instruction::Br); |
| 467 | |
| 468 | bool mustClearReg; |
| 469 | int valueToMove; |
| 470 | MachineOpCode movOpCode; |
| 471 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 472 | if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral()) |
| 473 | { |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 474 | // integer condition: destination should be %g0 or integer register |
| 475 | // if result must be saved but condition is not SetEQ then we need |
| 476 | // a separate instruction to compute the bool result, so discard |
| 477 | // result of SUBcc instruction anyway. |
| 478 | // |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 479 | mvec[0] = new MachineInstr(SUBcc); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 480 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult); |
| 481 | |
| 482 | if (discardResult) |
| 483 | {// mark the "result" operand as being a CC register |
| 484 | mvec[0]->SetMachineOperand(2, MachineOperand::MO_CCRegister, |
| 485 | subtreeRoot->getValue()); |
| 486 | } |
| 487 | else |
| 488 | { // recompute bool if needed, using the integer condition codes |
| 489 | if (result->getOpcode() == Instruction::SetNE) |
| 490 | discardResult = true; |
| 491 | else |
| 492 | movOpCode = |
| 493 | ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove); |
| 494 | } |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 495 | } |
| 496 | else |
| 497 | { |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 498 | // FP condition: dest of FCMP should be some FCCn register |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 499 | mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot)); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 500 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 501 | subtreeRoot->getValue()); |
| 502 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, |
| 503 | subtreeRoot->leftChild()->getValue()); |
| 504 | mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, |
| 505 | subtreeRoot->rightChild()->getValue()); |
| 506 | |
| 507 | if (!discardResult) |
| 508 | {// recompute bool using the FP condition codes |
| 509 | mustClearReg = true; |
| 510 | valueToMove = 1; |
| 511 | movOpCode = ChooseMovFpccInstruction(subtreeRoot); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if (!discardResult) |
| 516 | { |
| 517 | if (mustClearReg) |
| 518 | {// Unconditionally set register to 0 |
| 519 | int n = numInstr++; |
| 520 | mvec[n] = new MachineInstr(SETHI); |
| 521 | mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0); |
| 522 | mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, |
| 523 | subtreeRoot->getValue()); |
| 524 | } |
| 525 | |
| 526 | // Now conditionally move `valueToMove' (0 or 1) into the register |
| 527 | int n = numInstr++; |
| 528 | mvec[n] = new MachineInstr(movOpCode); |
| 529 | mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister, |
| 530 | subtreeRoot->getValue()); |
| 531 | mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed, |
| 532 | valueToMove); |
| 533 | mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, |
| 534 | subtreeRoot->getValue()); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 535 | } |
| 536 | break; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 539 | case 43: // boolreg: VReg |
Vikram S. Adve | 74f4a13 | 2001-07-31 21:46:57 +0000 | [diff] [blame^] | 540 | case 44: // boolreg: Constant |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 541 | numInstr = 0; |
| 542 | break; |
| 543 | |
| 544 | case 51: // reg: Load(reg) |
| 545 | case 52: // reg: Load(ptrreg) |
| 546 | case 53: // reg: LoadIdx(reg,reg) |
| 547 | case 54: // reg: LoadIdx(ptrreg,reg) |
| 548 | mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType())); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 549 | SetOperandsForMemInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | case 55: // reg: GetElemPtr(reg) |
| 553 | case 56: // reg: GetElemPtrIdx(reg,reg) |
| 554 | if (subtreeRoot->parent() != NULL) |
| 555 | { |
| 556 | // Check if the parent was an array access. |
| 557 | // If so, we still need to generate this instruction. |
| 558 | MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction(); |
| 559 | const PointerType* ptrType = |
| 560 | (const PointerType*) memInst->getPtrOperand()->getType(); |
| 561 | if (! ptrType->getValueType()->isArrayType()) |
| 562 | {// we don't need a separate instr |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 563 | numInstr = 0; // don't forward operand! |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | } |
| 567 | // else in all other cases we need to a separate ADD instruction |
| 568 | mvec[0] = new MachineInstr(ADD); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 569 | SetOperandsForMemInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case 57: // reg: Alloca: Implement as 2 instructions: |
| 573 | // sub %sp, tmp -> %sp |
| 574 | { // add %sp, 0 -> result |
| 575 | Instruction* instr = subtreeRoot->getInstruction(); |
| 576 | const PointerType* instrType = (const PointerType*) instr->getType(); |
| 577 | assert(instrType->isPointerType()); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 578 | int tsize = (int) target.findOptimalStorageSize(instrType->getValueType()); |
| 579 | assert(tsize != 0 && "Just to check when this can happen"); |
| 580 | // if (tsize == 0) |
| 581 | // { |
| 582 | // numInstr = 0; |
| 583 | // break; |
| 584 | // } |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 585 | //else go on to create the instructions needed... |
| 586 | |
| 587 | // Create a temporary Value to hold the constant type-size |
| 588 | ConstPoolSInt* valueForTSize = new ConstPoolSInt(Type::IntTy, tsize); |
| 589 | ConstantPool &cpool = instr->getParent()->getParent()->getConstantPool(); |
| 590 | if (cpool.find(valueForTSize) == 0) |
| 591 | cpool.insert(valueForTSize); |
| 592 | |
| 593 | // Instruction 1: sub %sp, tsize -> %sp |
| 594 | // tsize is always constant, but it may have to be put into a |
| 595 | // register if it doesn't fit in the immediate field. |
| 596 | // |
| 597 | mvec[0] = new MachineInstr(SUB); |
| 598 | mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 599 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 600 | mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14); |
| 601 | |
| 602 | // Instruction 2: add %sp, 0 -> result |
| 603 | numInstr++; |
| 604 | mvec[1] = new MachineInstr(ADD); |
| 605 | mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14); |
| 606 | mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 607 | mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | |
| 611 | case 58: // reg: Alloca(reg): Implement as 3 instructions: |
| 612 | // mul num, typeSz -> tmp |
| 613 | // sub %sp, tmp -> %sp |
| 614 | { // add %sp, 0 -> result |
| 615 | Instruction* instr = subtreeRoot->getInstruction(); |
| 616 | const PointerType* instrType = (const PointerType*) instr->getType(); |
| 617 | assert(instrType->isPointerType() && |
| 618 | instrType->getValueType()->isArrayType()); |
| 619 | const Type* eltType = |
| 620 | ((ArrayType*) instrType->getValueType())->getElementType(); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 621 | int tsize = (int) target.findOptimalStorageSize(eltType); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 622 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 623 | assert(tsize != 0 && "Just to check when this can happen"); |
| 624 | // if (tsize == 0) |
| 625 | // { |
| 626 | // numInstr = 0; |
| 627 | // break; |
| 628 | // } |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 629 | //else go on to create the instructions needed... |
| 630 | |
| 631 | // Create a temporary Value to hold the constant type-size |
| 632 | ConstPoolSInt* valueForTSize = new ConstPoolSInt(Type::IntTy, tsize); |
| 633 | ConstantPool &cpool = instr->getParent()->getParent()->getConstantPool(); |
| 634 | if (cpool.find(valueForTSize) == 0) |
| 635 | cpool.insert(valueForTSize); |
| 636 | |
| 637 | // Create a temporary value to hold `tmp' |
| 638 | Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1, |
| 639 | subtreeRoot->leftChild()->getValue(), |
| 640 | NULL /*could insert tsize here*/); |
| 641 | subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr); |
| 642 | |
| 643 | // Instruction 1: mul numElements, typeSize -> tmp |
| 644 | mvec[0] = new MachineInstr(MULX); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 645 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 646 | subtreeRoot->leftChild()->getValue()); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 647 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize); |
| 648 | mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 649 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 650 | tmpInstr->addMachineInstruction(mvec[0]); |
| 651 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 652 | // Instruction 2: sub %sp, tmp -> %sp |
| 653 | numInstr++; |
| 654 | mvec[1] = new MachineInstr(SUB); |
| 655 | mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 656 | mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 657 | mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14); |
| 658 | |
| 659 | // Instruction 3: add %sp, 0 -> result |
| 660 | numInstr++; |
| 661 | mvec[2] = new MachineInstr(ADD); |
| 662 | mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14); |
| 663 | mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 664 | mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 665 | break; |
| 666 | } |
| 667 | |
| 668 | case 61: // reg: Call |
| 669 | // Generate a call-indirect (i.e., JMPL) for now to expose |
| 670 | // the potential need for registers. If an absolute address |
| 671 | // is available, replace this with a CALL instruction. |
| 672 | // Mark both the indirection register and the return-address |
| 673 | { // register as hidden virtual registers. |
| 674 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 675 | Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 676 | ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 677 | Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1, |
| 678 | subtreeRoot->getValue(), NULL); |
| 679 | subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg); |
| 680 | subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 681 | |
| 682 | mvec[0] = new MachineInstr(JMPL); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 683 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 684 | mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, |
| 685 | (int64_t) 0); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 686 | mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg); |
| 687 | |
| 688 | // NOTE: jmpAddrReg will be loaded by a different instruction generated |
| 689 | // by the final code generator, so we just mark the CALL instruction |
| 690 | // as computing that value. |
| 691 | // The retAddrReg is actually computed by the CALL instruction. |
| 692 | // |
| 693 | jmpAddrReg->addMachineInstruction(mvec[0]); |
| 694 | retAddrReg->addMachineInstruction(mvec[0]); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 695 | |
| 696 | mvec[numInstr++] = new MachineInstr(NOP); // delay slot |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | case 62: // reg: Shl(reg, reg) |
| 701 | opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 702 | assert(opType->isIntegral() || opType == Type::BoolTy); |
| 703 | mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 704 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 705 | break; |
| 706 | |
| 707 | case 63: // reg: Shr(reg, reg) |
| 708 | opType = subtreeRoot->leftChild()->getValue()->getType(); |
| 709 | assert(opType->isIntegral() || opType == Type::BoolTy); |
| 710 | mvec[0] = new MachineInstr((opType->isSigned() |
| 711 | ? ((opType == Type::LongTy)? SRAX : SRA) |
| 712 | : ((opType == Type::LongTy)? SRLX : SRL))); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 713 | Set3OperandsFromInstr(mvec[0], subtreeRoot, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 714 | break; |
| 715 | |
Vikram S. Adve | 74f4a13 | 2001-07-31 21:46:57 +0000 | [diff] [blame^] | 716 | case 64: // reg: Phi(reg,reg) |
| 717 | { // This instruction has variable #operands, so resultPos is 0. |
| 718 | Instruction* phi = subtreeRoot->getInstruction(); |
| 719 | mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands()); |
| 720 | mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, |
| 721 | subtreeRoot->getValue()); |
| 722 | for (unsigned i=0, N=phi->getNumOperands(); i < N; i++) |
| 723 | mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister, |
| 724 | phi->getOperand(i)); |
| 725 | break; |
| 726 | } |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 727 | case 71: // reg: VReg |
| 728 | case 72: // reg: Constant |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 729 | numInstr = 0; // don't forward the value |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case 111: // stmt: reg |
| 733 | case 112: // stmt: boolconst |
| 734 | case 113: // stmt: bool |
| 735 | case 121: |
| 736 | case 122: |
| 737 | case 123: |
| 738 | case 124: |
| 739 | case 125: |
| 740 | case 126: |
| 741 | case 127: |
| 742 | case 128: |
| 743 | case 129: |
| 744 | case 130: |
| 745 | case 131: |
| 746 | case 132: |
| 747 | case 153: |
Vikram S. Adve | 74f4a13 | 2001-07-31 21:46:57 +0000 | [diff] [blame^] | 748 | case 155: |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 749 | // |
| 750 | // These are all chain rules, which have a single nonterminal on the RHS. |
| 751 | // Get the rule that matches the RHS non-terminal and use that instead. |
| 752 | // |
| 753 | assert(ThisIsAChainRule(ruleForNode)); |
| 754 | assert(nts[0] && ! nts[1] |
| 755 | && "A chain rule should have only one RHS non-terminal!"); |
| 756 | nextRule = burm_rule(subtreeRoot->getBasicNode()->state, nts[0]); |
| 757 | nts = burm_nts[nextRule]; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 758 | numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 759 | break; |
| 760 | |
| 761 | default: |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 762 | assert(0 && "Unrecognized BURG rule"); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 763 | numInstr = 0; |
| 764 | break; |
| 765 | } |
| 766 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 767 | if (forwardOperandNum >= 0) |
| 768 | { // We did not generate a machine instruction but need to use operand. |
| 769 | // If user is in the same tree, replace Value in its machine operand. |
| 770 | // If not, insert a copy instruction which should get coalesced away |
| 771 | // by register allocation. |
| 772 | if (subtreeRoot->parent() != NULL) |
| 773 | ForwardOperand(subtreeRoot, (InstructionNode*) subtreeRoot->parent(), |
| 774 | forwardOperandNum); |
| 775 | else |
| 776 | { |
| 777 | int n = numInstr++; |
| 778 | mvec[n] = new MachineInstr(ADD); |
| 779 | mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, |
| 780 | subtreeRoot->getInstruction()->getOperand(forwardOperandNum)); |
| 781 | mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0); |
| 782 | mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, |
| 783 | subtreeRoot->getInstruction()); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (! ThisIsAChainRule(ruleForNode)) |
| 788 | numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 789 | |
| 790 | return numInstr; |
| 791 | } |
| 792 | |
| 793 | |
| 794 | //--------------------------------------------------------------------------- |
| 795 | // Private helper routines for SPARC instruction selection. |
| 796 | //--------------------------------------------------------------------------- |
| 797 | |
| 798 | |
| 799 | static MachineOpCode |
| 800 | ChooseBprInstruction(const InstructionNode* instrNode) |
| 801 | { |
| 802 | MachineOpCode opCode; |
| 803 | |
| 804 | Instruction* setCCInstr = |
| 805 | ((InstructionNode*) instrNode->leftChild())->getInstruction(); |
| 806 | |
| 807 | switch(setCCInstr->getOpcode()) |
| 808 | { |
| 809 | case Instruction::SetEQ: opCode = BRZ; break; |
| 810 | case Instruction::SetNE: opCode = BRNZ; break; |
| 811 | case Instruction::SetLE: opCode = BRLEZ; break; |
| 812 | case Instruction::SetGE: opCode = BRGEZ; break; |
| 813 | case Instruction::SetLT: opCode = BRLZ; break; |
| 814 | case Instruction::SetGT: opCode = BRGZ; break; |
| 815 | default: |
| 816 | assert(0 && "Unrecognized VM instruction!"); |
| 817 | opCode = INVALID_OPCODE; |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | return opCode; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | static MachineOpCode |
| 826 | ChooseBccInstruction(const InstructionNode* instrNode, |
| 827 | bool& isFPBranch) |
| 828 | { |
| 829 | InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild(); |
| 830 | BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction(); |
| 831 | const Type* setCCType = setCCInstr->getOperand(0)->getType(); |
| 832 | |
| 833 | isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy); |
| 834 | |
| 835 | if (isFPBranch) |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 836 | return ChooseBFpccInstruction(instrNode, setCCInstr); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 837 | else |
| 838 | return ChooseBpccInstruction(instrNode, setCCInstr); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | static MachineOpCode |
| 843 | ChooseBpccInstruction(const InstructionNode* instrNode, |
| 844 | const BinaryOperator* setCCInstr) |
| 845 | { |
| 846 | MachineOpCode opCode = INVALID_OPCODE; |
| 847 | |
| 848 | bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned(); |
| 849 | |
| 850 | if (isSigned) |
| 851 | { |
| 852 | switch(setCCInstr->getOpcode()) |
| 853 | { |
| 854 | case Instruction::SetEQ: opCode = BE; break; |
| 855 | case Instruction::SetNE: opCode = BNE; break; |
| 856 | case Instruction::SetLE: opCode = BLE; break; |
| 857 | case Instruction::SetGE: opCode = BGE; break; |
| 858 | case Instruction::SetLT: opCode = BL; break; |
| 859 | case Instruction::SetGT: opCode = BG; break; |
| 860 | default: |
| 861 | assert(0 && "Unrecognized VM instruction!"); |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 | else |
| 866 | { |
| 867 | switch(setCCInstr->getOpcode()) |
| 868 | { |
| 869 | case Instruction::SetEQ: opCode = BE; break; |
| 870 | case Instruction::SetNE: opCode = BNE; break; |
| 871 | case Instruction::SetLE: opCode = BLEU; break; |
| 872 | case Instruction::SetGE: opCode = BCC; break; |
| 873 | case Instruction::SetLT: opCode = BCS; break; |
| 874 | case Instruction::SetGT: opCode = BGU; break; |
| 875 | default: |
| 876 | assert(0 && "Unrecognized VM instruction!"); |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | return opCode; |
| 882 | } |
| 883 | |
| 884 | static MachineOpCode |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 885 | ChooseBFpccInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 886 | const BinaryOperator* setCCInstr) |
| 887 | { |
| 888 | MachineOpCode opCode = INVALID_OPCODE; |
| 889 | |
| 890 | switch(setCCInstr->getOpcode()) |
| 891 | { |
| 892 | case Instruction::SetEQ: opCode = FBE; break; |
| 893 | case Instruction::SetNE: opCode = FBNE; break; |
| 894 | case Instruction::SetLE: opCode = FBLE; break; |
| 895 | case Instruction::SetGE: opCode = FBGE; break; |
| 896 | case Instruction::SetLT: opCode = FBL; break; |
| 897 | case Instruction::SetGT: opCode = FBG; break; |
| 898 | default: |
| 899 | assert(0 && "Unrecognized VM instruction!"); |
| 900 | break; |
| 901 | } |
| 902 | |
| 903 | return opCode; |
| 904 | } |
| 905 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 906 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 907 | static MachineOpCode |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 908 | ChooseMovFpccInstruction(const InstructionNode* instrNode) |
| 909 | { |
| 910 | MachineOpCode opCode = INVALID_OPCODE; |
| 911 | |
| 912 | switch(instrNode->getInstruction()->getOpcode()) |
| 913 | { |
| 914 | case Instruction::SetEQ: opCode = MOVFE; break; |
| 915 | case Instruction::SetNE: opCode = MOVFNE; break; |
| 916 | case Instruction::SetLE: opCode = MOVFLE; break; |
| 917 | case Instruction::SetGE: opCode = MOVFGE; break; |
| 918 | case Instruction::SetLT: opCode = MOVFL; break; |
| 919 | case Instruction::SetGT: opCode = MOVFG; break; |
| 920 | default: |
| 921 | assert(0 && "Unrecognized VM instruction!"); |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | return opCode; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | // Assumes that SUBcc v1, v2 -> v3 has been executed. |
| 930 | // In most cases, we want to clear v3 and then follow it by instruction |
| 931 | // MOVcc 1 -> v3. |
| 932 | // Set mustClearReg=false if v3 need not be cleared before conditional move. |
| 933 | // Set valueToMove=0 if we want to conditionally move 0 instead of 1 |
| 934 | // (i.e., we want to test inverse of a condition) |
| 935 | // |
| 936 | // |
| 937 | static MachineOpCode |
| 938 | ChooseMovpccAfterSub(const InstructionNode* instrNode, |
| 939 | bool& mustClearReg, |
| 940 | int& valueToMove) |
| 941 | { |
| 942 | MachineOpCode opCode = INVALID_OPCODE; |
| 943 | mustClearReg = true; |
| 944 | valueToMove = 1; |
| 945 | |
| 946 | switch(instrNode->getInstruction()->getOpcode()) |
| 947 | { |
| 948 | case Instruction::SetEQ: opCode = MOVNE; mustClearReg = false; |
| 949 | valueToMove = 0; break; |
| 950 | case Instruction::SetLE: opCode = MOVLE; break; |
| 951 | case Instruction::SetGE: opCode = MOVGE; break; |
| 952 | case Instruction::SetLT: opCode = MOVL; break; |
| 953 | case Instruction::SetGT: opCode = MOVG; break; |
| 954 | |
| 955 | case Instruction::SetNE: assert(0 && "No move required!"); |
| 956 | |
| 957 | default: |
| 958 | assert(0 && "Unrecognized VM instruction!"); |
| 959 | break; |
| 960 | } |
| 961 | |
| 962 | return opCode; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | static MachineOpCode |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 967 | ChooseConvertToFloatInstr(const InstructionNode* instrNode, |
| 968 | const Type* opType) |
| 969 | { |
| 970 | MachineOpCode opCode = INVALID_OPCODE; |
| 971 | |
| 972 | switch(instrNode->getOpLabel()) |
| 973 | { |
| 974 | case ToFloatTy: |
| 975 | if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy) |
| 976 | opCode = FITOS; |
| 977 | else if (opType == Type::LongTy) |
| 978 | opCode = FXTOS; |
| 979 | else if (opType == Type::DoubleTy) |
| 980 | opCode = FDTOS; |
| 981 | else |
| 982 | assert(0 && "Cannot convert this type to FLOAT on SPARC"); |
| 983 | break; |
| 984 | |
| 985 | case ToDoubleTy: |
| 986 | if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy) |
| 987 | opCode = FITOD; |
| 988 | else if (opType == Type::LongTy) |
| 989 | opCode = FXTOD; |
| 990 | else if (opType == Type::FloatTy) |
| 991 | opCode = FSTOD; |
| 992 | else |
| 993 | assert(0 && "Cannot convert this type to DOUBLE on SPARC"); |
| 994 | break; |
| 995 | |
| 996 | default: |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | return opCode; |
| 1001 | } |
| 1002 | |
| 1003 | static MachineOpCode |
| 1004 | ChooseConvertToIntInstr(const InstructionNode* instrNode, |
| 1005 | const Type* opType) |
| 1006 | { |
| 1007 | MachineOpCode opCode = INVALID_OPCODE;; |
| 1008 | |
| 1009 | int instrType = (int) instrNode->getOpLabel(); |
| 1010 | |
| 1011 | if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy) |
| 1012 | { |
| 1013 | switch (opType->getPrimitiveID()) |
| 1014 | { |
| 1015 | case Type::FloatTyID: opCode = FSTOI; break; |
| 1016 | case Type::DoubleTyID: opCode = FDTOI; break; |
| 1017 | default: |
| 1018 | assert(0 && "Non-numeric non-bool type cannot be converted to Int"); |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | else if (instrType == ToLongTy) |
| 1023 | { |
| 1024 | switch (opType->getPrimitiveID()) |
| 1025 | { |
| 1026 | case Type::FloatTyID: opCode = FSTOX; break; |
| 1027 | case Type::DoubleTyID: opCode = FDTOX; break; |
| 1028 | default: |
| 1029 | assert(0 && "Non-numeric non-bool type cannot be converted to Long"); |
| 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | else |
| 1034 | assert(0 && "Should not get here, Mo!"); |
| 1035 | |
| 1036 | return opCode; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | static MachineOpCode |
| 1041 | ChooseAddInstruction(const InstructionNode* instrNode) |
| 1042 | { |
| 1043 | MachineOpCode opCode = INVALID_OPCODE; |
| 1044 | |
| 1045 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 1046 | |
| 1047 | if (resultType->isIntegral() || |
| 1048 | resultType->isPointerType() || |
| 1049 | resultType->isMethodType() || |
| 1050 | resultType->isLabelType()) |
| 1051 | { |
| 1052 | opCode = ADD; |
| 1053 | } |
| 1054 | else |
| 1055 | { |
| 1056 | Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue(); |
| 1057 | switch(operand->getType()->getPrimitiveID()) |
| 1058 | { |
| 1059 | case Type::FloatTyID: opCode = FADDS; break; |
| 1060 | case Type::DoubleTyID: opCode = FADDD; break; |
| 1061 | default: assert(0 && "Invalid type for ADD instruction"); break; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | return opCode; |
| 1066 | } |
| 1067 | |
| 1068 | static MachineOpCode |
| 1069 | ChooseSubInstruction(const InstructionNode* instrNode) |
| 1070 | { |
| 1071 | MachineOpCode opCode = INVALID_OPCODE; |
| 1072 | |
| 1073 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 1074 | |
| 1075 | if (resultType->isIntegral() || |
| 1076 | resultType->isPointerType()) |
| 1077 | { |
| 1078 | opCode = SUB; |
| 1079 | } |
| 1080 | else |
| 1081 | { |
| 1082 | Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue(); |
| 1083 | switch(operand->getType()->getPrimitiveID()) |
| 1084 | { |
| 1085 | case Type::FloatTyID: opCode = FSUBS; break; |
| 1086 | case Type::DoubleTyID: opCode = FSUBD; break; |
| 1087 | default: assert(0 && "Invalid type for SUB instruction"); break; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | return opCode; |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | static MachineOpCode |
| 1096 | ChooseFcmpInstruction(const InstructionNode* instrNode) |
| 1097 | { |
| 1098 | MachineOpCode opCode = INVALID_OPCODE; |
| 1099 | |
| 1100 | Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue(); |
| 1101 | switch(operand->getType()->getPrimitiveID()) |
| 1102 | { |
| 1103 | case Type::FloatTyID: opCode = FCMPS; break; |
| 1104 | case Type::DoubleTyID: opCode = FCMPD; break; |
| 1105 | default: assert(0 && "Invalid type for ADD instruction"); break; |
| 1106 | } |
| 1107 | |
| 1108 | return opCode; |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | static MachineOpCode |
| 1113 | ChooseMulInstruction(const InstructionNode* instrNode, |
| 1114 | bool checkCasts) |
| 1115 | { |
| 1116 | MachineOpCode opCode = INVALID_OPCODE; |
| 1117 | |
| 1118 | if (checkCasts) |
| 1119 | { |
| 1120 | // Assume that leftArg and rightArg are both cast instructions. |
| 1121 | // |
| 1122 | InstrTreeNode* leftArg = instrNode->leftChild(); |
| 1123 | InstrTreeNode* rightArg = instrNode->rightChild(); |
| 1124 | InstrTreeNode* leftArgArg = leftArg->leftChild(); |
| 1125 | InstrTreeNode* rightArgArg = rightArg->leftChild(); |
| 1126 | assert(leftArg->getValue()->getType() ==rightArg->getValue()->getType()); |
| 1127 | |
| 1128 | // If both arguments are floats cast to double, use FsMULd |
| 1129 | if (leftArg->getValue()->getType() == Type::DoubleTy && |
| 1130 | leftArgArg->getValue()->getType() == Type::FloatTy && |
| 1131 | rightArgArg->getValue()->getType() == Type::FloatTy) |
| 1132 | { |
| 1133 | return opCode = FSMULD; |
| 1134 | } |
| 1135 | // else fall through and use the regular multiply instructions |
| 1136 | } |
| 1137 | |
| 1138 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 1139 | |
| 1140 | if (resultType->isIntegral()) |
| 1141 | { |
| 1142 | opCode = MULX; |
| 1143 | } |
| 1144 | else |
| 1145 | { |
| 1146 | switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID()) |
| 1147 | { |
| 1148 | case Type::FloatTyID: opCode = FMULS; break; |
| 1149 | case Type::DoubleTyID: opCode = FMULD; break; |
| 1150 | default: assert(0 && "Invalid type for MUL instruction"); break; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return opCode; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | static MachineOpCode |
| 1159 | ChooseDivInstruction(const InstructionNode* instrNode) |
| 1160 | { |
| 1161 | MachineOpCode opCode = INVALID_OPCODE; |
| 1162 | |
| 1163 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 1164 | |
| 1165 | if (resultType->isIntegral()) |
| 1166 | { |
| 1167 | opCode = resultType->isSigned()? SDIVX : UDIVX; |
| 1168 | } |
| 1169 | else |
| 1170 | { |
| 1171 | Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue(); |
| 1172 | switch(operand->getType()->getPrimitiveID()) |
| 1173 | { |
| 1174 | case Type::FloatTyID: opCode = FDIVS; break; |
| 1175 | case Type::DoubleTyID: opCode = FDIVD; break; |
| 1176 | default: assert(0 && "Invalid type for DIV instruction"); break; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return opCode; |
| 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | static MachineOpCode |
| 1185 | ChooseLoadInstruction(const Type* resultType) |
| 1186 | { |
| 1187 | MachineOpCode opCode = INVALID_OPCODE; |
| 1188 | |
| 1189 | switch (resultType->getPrimitiveID()) |
| 1190 | { |
| 1191 | case Type::BoolTyID: opCode = LDUB; break; |
| 1192 | case Type::UByteTyID: opCode = LDUB; break; |
| 1193 | case Type::SByteTyID: opCode = LDSB; break; |
| 1194 | case Type::UShortTyID: opCode = LDUH; break; |
| 1195 | case Type::ShortTyID: opCode = LDSH; break; |
| 1196 | case Type::UIntTyID: opCode = LDUW; break; |
| 1197 | case Type::IntTyID: opCode = LDSW; break; |
| 1198 | case Type::ULongTyID: |
| 1199 | case Type::LongTyID: opCode = LDX; break; |
| 1200 | case Type::FloatTyID: opCode = LD; break; |
| 1201 | case Type::DoubleTyID: opCode = LDD; break; |
| 1202 | default: assert(0 && "Invalid type for Load instruction"); break; |
| 1203 | } |
| 1204 | |
| 1205 | return opCode; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | static MachineOpCode |
| 1210 | ChooseStoreInstruction(const Type* valueType) |
| 1211 | { |
| 1212 | MachineOpCode opCode = INVALID_OPCODE; |
| 1213 | |
| 1214 | switch (valueType->getPrimitiveID()) |
| 1215 | { |
| 1216 | case Type::BoolTyID: |
| 1217 | case Type::UByteTyID: |
| 1218 | case Type::SByteTyID: opCode = STB; break; |
| 1219 | case Type::UShortTyID: |
| 1220 | case Type::ShortTyID: opCode = STH; break; |
| 1221 | case Type::UIntTyID: |
| 1222 | case Type::IntTyID: opCode = STW; break; |
| 1223 | case Type::ULongTyID: |
| 1224 | case Type::LongTyID: opCode = STX; break; |
| 1225 | case Type::FloatTyID: opCode = ST; break; |
| 1226 | case Type::DoubleTyID: opCode = STD; break; |
| 1227 | default: assert(0 && "Invalid type for Store instruction"); break; |
| 1228 | } |
| 1229 | |
| 1230 | return opCode; |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | //------------------------------------------------------------------------ |
| 1235 | // Function SetOperandsForMemInstr |
| 1236 | // |
| 1237 | // Choose addressing mode for the given load or store instruction. |
| 1238 | // Use [reg+reg] if it is an indexed reference, and the index offset is |
| 1239 | // not a constant or if it cannot fit in the offset field. |
| 1240 | // Use [reg+offset] in all other cases. |
| 1241 | // |
| 1242 | // This assumes that all array refs are "lowered" to one of these forms: |
| 1243 | // %x = load (subarray*) ptr, constant ; single constant offset |
| 1244 | // %x = load (subarray*) ptr, offsetVal ; single non-constant offset |
| 1245 | // Generally, this should happen via strength reduction + LICM. |
| 1246 | // Also, strength reduction should take care of using the same register for |
| 1247 | // the loop index variable and an array index, when that is profitable. |
| 1248 | //------------------------------------------------------------------------ |
| 1249 | |
| 1250 | static void |
| 1251 | SetOperandsForMemInstr(MachineInstr* minstr, |
| 1252 | const InstructionNode* vmInstrNode, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1253 | const TargetMachine& target) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1254 | { |
| 1255 | MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction(); |
| 1256 | |
| 1257 | // Variables to hold the index vector, ptr value, and offset value. |
| 1258 | // The major work here is to extract these for all 3 instruction types |
| 1259 | // and then call the common function SetMemOperands_Internal(). |
| 1260 | // |
| 1261 | const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec(); |
| 1262 | vector<ConstPoolVal*>* newIdxVec = NULL; |
| 1263 | Value* ptrVal; |
| 1264 | Value* arrayOffsetVal = NULL; |
| 1265 | |
| 1266 | // Test if a GetElemPtr instruction is being folded into this mem instrn. |
| 1267 | // If so, it will be in the left child for Load and GetElemPtr, |
| 1268 | // and in the right child for Store instructions. |
| 1269 | // |
| 1270 | InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store |
| 1271 | ? vmInstrNode->rightChild() |
| 1272 | : vmInstrNode->leftChild()); |
| 1273 | |
| 1274 | if (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 1275 | ptrChild->getOpLabel() == GetElemPtrIdx) |
| 1276 | { |
| 1277 | // There is a GetElemPtr instruction and there may be a chain of |
| 1278 | // more than one. Use the pointer value of the last one in the chain. |
| 1279 | // Fold the index vectors from the entire chain and from the mem |
| 1280 | // instruction into one single index vector. |
| 1281 | // Finally, we never fold for an array instruction so make that NULL. |
| 1282 | |
| 1283 | newIdxVec = new vector<ConstPoolVal*>; |
| 1284 | ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec); |
| 1285 | |
| 1286 | newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end()); |
| 1287 | idxVec = newIdxVec; |
| 1288 | |
| 1289 | assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType() |
| 1290 | && "GetElemPtr cannot be folded into array refs in selection"); |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | // There is no GetElemPtr instruction. |
| 1295 | // Use the pointer value and the index vector from the Mem instruction. |
| 1296 | // If it is an array reference, get the array offset value. |
| 1297 | // |
| 1298 | ptrVal = memInst->getPtrOperand(); |
| 1299 | |
| 1300 | const Type* opType = |
| 1301 | ((const PointerType*) ptrVal->getType())->getValueType(); |
| 1302 | if (opType->isArrayType()) |
| 1303 | { |
| 1304 | assert((memInst->getNumOperands() |
| 1305 | == (unsigned) 1 + memInst->getFirstOffsetIdx()) |
| 1306 | && "Array refs must be lowered before Instruction Selection"); |
| 1307 | |
| 1308 | arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx()); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1313 | *idxVec, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1314 | |
| 1315 | if (newIdxVec != NULL) |
| 1316 | delete newIdxVec; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | static void |
| 1321 | SetMemOperands_Internal(MachineInstr* minstr, |
| 1322 | const InstructionNode* vmInstrNode, |
| 1323 | Value* ptrVal, |
| 1324 | Value* arrayOffsetVal, |
| 1325 | const vector<ConstPoolVal*>& idxVec, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1326 | const TargetMachine& target) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1327 | { |
| 1328 | MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction(); |
| 1329 | |
| 1330 | // Initialize so we default to storing the offset in a register. |
| 1331 | int64_t smallConstOffset; |
| 1332 | Value* valueForRegOffset = NULL; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1333 | MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister; |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1334 | |
| 1335 | // Check if there is an index vector and if so, if it translates to |
| 1336 | // a small enough constant to fit in the immediate-offset field. |
| 1337 | // |
| 1338 | if (idxVec.size() > 0) |
| 1339 | { |
| 1340 | bool isConstantOffset = false; |
| 1341 | unsigned offset; |
| 1342 | |
| 1343 | const PointerType* ptrType = (PointerType*) ptrVal->getType(); |
| 1344 | |
| 1345 | if (ptrType->getValueType()->isStructType()) |
| 1346 | { |
| 1347 | // the offset is always constant for structs |
| 1348 | isConstantOffset = true; |
| 1349 | |
| 1350 | // Compute the offset value using the index vector |
| 1351 | offset = MemAccessInst::getIndexedOfsetForTarget(ptrType, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1352 | idxVec, target); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1353 | } |
| 1354 | else |
| 1355 | { |
| 1356 | // It must be an array ref. Check if the offset is a constant, |
| 1357 | // and that the indexing has been lowered to a single offset. |
| 1358 | // |
| 1359 | assert(ptrType->getValueType()->isArrayType()); |
| 1360 | assert(arrayOffsetVal != NULL |
| 1361 | && "Expect to be given Value* for array offsets"); |
| 1362 | |
| 1363 | if (arrayOffsetVal->getValueType() == Value::ConstantVal) |
| 1364 | { |
| 1365 | isConstantOffset = true; // always constant for structs |
| 1366 | assert(arrayOffsetVal->getType()->isIntegral()); |
| 1367 | offset = (arrayOffsetVal->getType()->isSigned()) |
| 1368 | ? ((ConstPoolSInt*) arrayOffsetVal)->getValue() |
| 1369 | : (int64_t) ((ConstPoolUInt*) arrayOffsetVal)->getValue(); |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | valueForRegOffset = arrayOffsetVal; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (isConstantOffset) |
| 1378 | { |
| 1379 | // create a virtual register for the constant |
| 1380 | valueForRegOffset = new ConstPoolSInt(Type::IntTy, offset); |
| 1381 | } |
| 1382 | } |
| 1383 | else |
| 1384 | { |
| 1385 | offsetOpType = MachineOperand::MO_SignExtendedImmed; |
| 1386 | smallConstOffset = 0; |
| 1387 | } |
| 1388 | |
| 1389 | // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR |
| 1390 | // It is the left child in the instruction tree in all cases. |
| 1391 | Value* leftVal = vmInstrNode->leftChild()->getValue(); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1392 | minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR |
| 1395 | // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR |
| 1396 | // |
| 1397 | unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1398 | if (offsetOpType == MachineOperand::MO_VirtualRegister) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1399 | { |
| 1400 | assert(valueForRegOffset != NULL); |
| 1401 | minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset); |
| 1402 | } |
| 1403 | else |
| 1404 | minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset); |
| 1405 | |
| 1406 | if (memInst->getOpcode() == Instruction::Store) |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1407 | minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1408 | else |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1409 | minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1410 | vmInstrNode->getValue()); |
| 1411 | } |
| 1412 | |
| 1413 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1414 | // Special handling for constant operands: |
| 1415 | // -- if the constant is 0, use the hardwired 0 register, if any |
| 1416 | // -- if the constant fits in the IMMEDIATE field, use that field |
| 1417 | // -- else insert instructions to put the constant into a register, either |
| 1418 | // directly or by loading explicitly from the constant pool. |
| 1419 | // |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1420 | static unsigned |
| 1421 | FixConstantOperands(const InstructionNode* vmInstrNode, |
| 1422 | MachineInstr** mvec, |
| 1423 | unsigned numInstr, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1424 | TargetMachine& target) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1425 | { |
| 1426 | static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR]; |
| 1427 | |
| 1428 | unsigned numNew = 0; |
| 1429 | Instruction* vmInstr = vmInstrNode->getInstruction(); |
| 1430 | |
| 1431 | for (unsigned i=0; i < numInstr; i++) |
| 1432 | { |
| 1433 | MachineInstr* minstr = mvec[i]; |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1434 | const MachineInstrDescriptor& instrDesc = |
| 1435 | target.getInstrInfo().getDescriptor(minstr->getOpCode()); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1436 | |
Vikram S. Adve | 74f4a13 | 2001-07-31 21:46:57 +0000 | [diff] [blame^] | 1437 | for (unsigned op=0; op < minstr->getNumOperands(); op++) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1438 | { |
| 1439 | const MachineOperand& mop = minstr->getOperand(op); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1440 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1441 | // skip the result position (for efficiency below) and any other |
| 1442 | // positions already marked as not a virtual register |
| 1443 | if (instrDesc.resultPos == (int) op || |
| 1444 | mop.getOperandType() != MachineOperand::MO_VirtualRegister || |
| 1445 | mop.getVRegValue() == NULL) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1446 | { |
| 1447 | break; |
| 1448 | } |
| 1449 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1450 | Value* opValue = mop.getVRegValue(); |
| 1451 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1452 | if (opValue->getValueType() == Value::ConstantVal) |
| 1453 | { |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1454 | unsigned int machineRegNum; |
| 1455 | int64_t immedValue; |
| 1456 | MachineOperand::MachineOperandType opType = |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1457 | ChooseRegOrImmed(opValue, minstr->getOpCode(), target, |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1458 | /*canUseImmed*/ (op == 1), |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1459 | machineRegNum, immedValue); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1460 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1461 | if (opType == MachineOperand::MO_MachineRegister) |
| 1462 | minstr->SetMachineOperand(op, machineRegNum); |
| 1463 | else if (opType == MachineOperand::MO_VirtualRegister) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1464 | { |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1465 | // value is constant and must be loaded into a register |
| 1466 | TmpInstruction* tmpReg; |
| 1467 | loadConstVec[numNew++] = |
| 1468 | MakeOneLoadConstInstr(vmInstr, opValue, tmpReg); |
| 1469 | minstr->SetMachineOperand(op, opType, tmpReg); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1470 | } |
| 1471 | else |
| 1472 | minstr->SetMachineOperand(op, opType, immedValue); |
| 1473 | } |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | if (numNew > 0) |
| 1478 | { |
| 1479 | // Insert the new instructions *before* the old ones by moving |
| 1480 | // the old ones over `numNew' positions (last-to-first, of course!). |
| 1481 | // |
| 1482 | for (int i=numInstr-1; i >= ((int) numInstr) - (int) numNew; i--) |
| 1483 | mvec[i+numNew] = mvec[i]; |
| 1484 | |
| 1485 | for (unsigned i=0; i < numNew; i++) |
| 1486 | mvec[i] = loadConstVec[i]; |
| 1487 | } |
| 1488 | |
| 1489 | return (numInstr + numNew); |
| 1490 | } |
| 1491 | |
| 1492 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1493 | #if 0 |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1494 | // Create one or two load instructions to load constants from the |
| 1495 | // constant pool. The first instructions is stored in instrA; |
| 1496 | // the second (if any) in instrB. |
| 1497 | // |
| 1498 | static unsigned |
| 1499 | InsertLoadConstInstructions(unsigned loadConstFlags, |
| 1500 | const InstructionNode* vmInstrNode, |
| 1501 | MachineInstr** mvec, |
| 1502 | unsigned numInstr) |
| 1503 | { |
| 1504 | MachineInstr *instrA = NULL, *instrB = NULL; |
| 1505 | |
| 1506 | unsigned numNew = 0; |
| 1507 | |
| 1508 | if (loadConstFlags & 0x01) |
| 1509 | { |
| 1510 | instrA = MakeOneLoadConstInstr(vmInstrNode->getInstruction(), |
| 1511 | vmInstrNode->leftChild()->getValue()); |
| 1512 | numNew++; |
| 1513 | } |
| 1514 | |
| 1515 | if (loadConstFlags & 0x02) |
| 1516 | { |
| 1517 | instrB = MakeOneLoadConstInstr(vmInstrNode->getInstruction(), |
| 1518 | vmInstrNode->rightChild()->getValue()); |
| 1519 | numNew++; |
| 1520 | } |
| 1521 | |
| 1522 | // Now insert the new instructions *before* the old ones by |
| 1523 | // moving the old ones over `numNew' positions (last-to-first, of course!). |
| 1524 | // |
| 1525 | for (int i=numInstr-1; i >= ((int) numInstr) - (int) numNew; i--) |
| 1526 | mvec[i+numNew] = mvec[i]; |
| 1527 | |
| 1528 | unsigned whichNew = 0; |
| 1529 | if (instrA != NULL) |
| 1530 | mvec[whichNew++] = instrA; |
| 1531 | if (instrB != NULL) |
| 1532 | mvec[whichNew++] = instrB; |
| 1533 | assert(whichNew == numNew); |
| 1534 | |
| 1535 | return numInstr + numNew; |
| 1536 | } |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1537 | #endif |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1538 | |
| 1539 | |
| 1540 | static MachineInstr* |
| 1541 | MakeOneLoadConstInstr(Instruction* vmInstr, |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1542 | Value* val, |
| 1543 | TmpInstruction*& tmpReg) |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1544 | { |
| 1545 | assert(val->getValueType() == Value::ConstantVal); |
| 1546 | |
| 1547 | MachineInstr* minstr; |
| 1548 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1549 | // Create a TmpInstruction to mark the hidden register used for the constant |
| 1550 | tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL); |
| 1551 | vmInstr->getMachineInstrVec().addTempValue(tmpReg); |
| 1552 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1553 | // Use a "set" instruction for known constants that can go in an integer reg. |
| 1554 | // Use a "load" instruction for all other constants, in particular, |
| 1555 | // floating point constants. |
| 1556 | // |
| 1557 | const Type* valType = val->getType(); |
| 1558 | if (valType->isIntegral() || |
| 1559 | valType->isPointerType() || |
| 1560 | valType == Type::BoolTy) |
| 1561 | { |
| 1562 | bool isValidConstant; |
| 1563 | if (val->getType()->isSigned()) |
| 1564 | { |
| 1565 | minstr = new MachineInstr(SETSW); |
| 1566 | minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, |
| 1567 | GetSignedIntConstantValue(val, isValidConstant)); |
| 1568 | } |
| 1569 | else |
| 1570 | { |
| 1571 | minstr = new MachineInstr(SETUW); |
| 1572 | minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, |
| 1573 | GetUnsignedIntConstantValue(val, isValidConstant)); |
| 1574 | } |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1575 | minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1576 | assert(isValidConstant && "Unrecognized constant"); |
| 1577 | } |
| 1578 | else |
| 1579 | { |
| 1580 | assert(valType == Type::FloatTy || |
| 1581 | valType == Type::DoubleTy); |
| 1582 | |
| 1583 | int64_t zeroOffset = 0; // to avoid overloading ambiguity with (Value*) 0 |
| 1584 | |
| 1585 | // Make a Load instruction, and make `val' both the ptr value *and* |
| 1586 | // the result value, and set the offset field to 0. Final code |
| 1587 | // generation will have to generate the base+offset for the constant. |
| 1588 | // |
| 1589 | minstr = new MachineInstr(ChooseLoadInstruction(val->getType())); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1590 | minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1591 | minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, |
| 1592 | zeroOffset); |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1593 | minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, tmpReg); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1596 | tmpReg->addMachineInstruction(minstr); |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1597 | |
| 1598 | return minstr; |
| 1599 | } |
| 1600 | |
Vikram S. Adve | 4f23166 | 2001-07-28 04:15:15 +0000 | [diff] [blame] | 1601 | // |
| 1602 | // Substitute operand `operandNum' of the instruction in node `treeNode' |
| 1603 | // in place the use(s) of that instruction in node `parent'. |
| 1604 | // |
| 1605 | static void |
| 1606 | ForwardOperand(InstructionNode* treeNode, |
| 1607 | InstructionNode* parent, |
| 1608 | int operandNum) |
| 1609 | { |
| 1610 | Instruction* unusedOp = treeNode->getInstruction(); |
| 1611 | Value* fwdOp = unusedOp->getOperand(operandNum); |
| 1612 | Instruction* userInstr = parent->getInstruction(); |
| 1613 | MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec(); |
| 1614 | for (unsigned i=0, N=mvec.size(); i < N; i++) |
| 1615 | { |
| 1616 | MachineInstr* minstr = mvec[i]; |
| 1617 | for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++) |
| 1618 | { |
| 1619 | const MachineOperand& mop = minstr->getOperand(i); |
| 1620 | if (mop.getOperandType() == MachineOperand::MO_VirtualRegister && |
| 1621 | mop.getVRegValue() == unusedOp) |
| 1622 | { |
| 1623 | minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister, |
| 1624 | fwdOp); |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
Vikram S. Adve | a21cf20 | 2001-07-21 12:42:19 +0000 | [diff] [blame] | 1630 | |
| 1631 | // This function is currently unused and incomplete but will be |
| 1632 | // used if we have a linear layout of basic blocks in LLVM code. |
| 1633 | // It decides which branch should fall-through, and whether an |
| 1634 | // extra unconditional branch is needed (when neither falls through). |
| 1635 | // |
| 1636 | void |
| 1637 | ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern) |
| 1638 | { |
| 1639 | BranchInst* brInstr = (BranchInst*) vmInstr; |
| 1640 | |
| 1641 | brPattern.flipCondition = false; |
| 1642 | brPattern.targetBB = brInstr->getSuccessor(0); |
| 1643 | brPattern.extraBranch = NULL; |
| 1644 | |
| 1645 | assert(brInstr->getNumSuccessors() > 1 && |
| 1646 | "Unnecessary analysis for unconditional branch"); |
| 1647 | |
| 1648 | assert(0 && "Fold branches in peephole optimization"); |
| 1649 | } |
| 1650 | |