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