blob: 95cc0ec62be9e6306dbbe0a914edd1e1501e266b [file] [log] [blame]
Vikram S. Advea21cf202001-07-21 12:42:19 +00001// $Id$
2//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
7//
8// History:
9// 7/02/01 - Vikram Adve - Created
10//***************************************************************************
11
Vikram S. Advea21cf202001-07-21 12:42:19 +000012#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 Lattner7e583cf2001-07-21 20:58:30 +000024#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. Advea21cf202001-07-21 12:42:19 +000028
29
30//******************** Internal Data Declarations ************************/
31
32// to be used later
33struct 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. Adve4f231662001-07-28 04:15:15 +000043static MachineOpCode ChooseBprInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000044
Vikram S. Adve4f231662001-07-28 04:15:15 +000045static MachineOpCode ChooseBccInstruction (const InstructionNode* instrNode,
46 bool& isFPBranch);
Vikram S. Advea21cf202001-07-21 12:42:19 +000047
Vikram S. Adve4f231662001-07-28 04:15:15 +000048static MachineOpCode ChooseBpccInstruction (const InstructionNode* instrNode,
49 const BinaryOperator* setCCInst);
Vikram S. Advea21cf202001-07-21 12:42:19 +000050
Vikram S. Adve4f231662001-07-28 04:15:15 +000051static MachineOpCode ChooseBFpccInstruction (const InstructionNode* instrNode,
52 const BinaryOperator* setCCInst);
Vikram S. Advea21cf202001-07-21 12:42:19 +000053
Vikram S. Adve4f231662001-07-28 04:15:15 +000054static MachineOpCode ChooseMovFpccInstruction(const InstructionNode*);
55
56static MachineOpCode ChooseMovpccAfterSub (const InstructionNode* instrNode,
57 bool& mustClearReg,
58 int& valueToMove);
59
60static MachineOpCode ChooseConvertToFloatInstr(const InstructionNode*,
Chris Lattner51a9ad92001-07-21 22:57:05 +000061 const Type* opType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000062
Vikram S. Adve4f231662001-07-28 04:15:15 +000063static MachineOpCode ChooseConvertToIntInstr(const InstructionNode* instrNode,
64 const Type* opType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000065
Vikram S. Adve4f231662001-07-28 04:15:15 +000066static MachineOpCode ChooseAddInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000067
Vikram S. Adve4f231662001-07-28 04:15:15 +000068static MachineOpCode ChooseSubInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000069
Vikram S. Adve4f231662001-07-28 04:15:15 +000070static MachineOpCode ChooseFcmpInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000071
Vikram S. Adve4f231662001-07-28 04:15:15 +000072static MachineOpCode ChooseMulInstruction (const InstructionNode* instrNode,
73 bool checkCasts);
Vikram S. Advea21cf202001-07-21 12:42:19 +000074
Vikram S. Adve4f231662001-07-28 04:15:15 +000075static MachineOpCode ChooseDivInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000076
Vikram S. Adve4f231662001-07-28 04:15:15 +000077static MachineOpCode ChooseLoadInstruction (const Type* resultType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000078
Vikram S. Adve4f231662001-07-28 04:15:15 +000079static MachineOpCode ChooseStoreInstruction (const Type* valueType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000080
Vikram S. Adve4f231662001-07-28 04:15:15 +000081static void SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Advea21cf202001-07-21 12:42:19 +000082 const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +000083 const TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000084
85static void SetMemOperands_Internal (MachineInstr* minstr,
86 const InstructionNode* vmInstrNode,
87 Value* ptrVal,
88 Value* arrayOffsetVal,
89 const vector<ConstPoolVal*>& idxVec,
Vikram S. Adve4f231662001-07-28 04:15:15 +000090 const TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000091
92static unsigned FixConstantOperands(const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +000093 MachineInstr** mvec,
94 unsigned numInstr,
95 TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000096
97static unsigned InsertLoadConstInstructions(unsigned loadConstFlags,
98 const InstructionNode* vmInstrNode,
99 MachineInstr** mvec,
100 unsigned numInstr);
101
102static MachineInstr* MakeOneLoadConstInstr(Instruction* vmInstr,
Vikram S. Adve4f231662001-07-28 04:15:15 +0000103 Value* val,
104 TmpInstruction*& tmpReg);
105
106static void ForwardOperand (InstructionNode* treeNode,
107 InstructionNode* parent,
108 int operandNum);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000109
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
121extern bool
122ThisIsAChainRule(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. Adve74f4a132001-07-31 21:46:57 +0000141 case 153:
142 case 155: return true; break;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000143
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
156unsigned
157GetInstructionsByRule(InstructionNode* subtreeRoot,
158 int ruleForNode,
159 short* nts,
Vikram S. Adve4f231662001-07-28 04:15:15 +0000160 TargetMachine &target,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000161 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. Adve4f231662001-07-28 04:15:15 +0000168 int forwardOperandNum = -1;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000169 BranchPattern brPattern;
Vikram S. Adve4f231662001-07-28 04:15:15 +0000170 int64_t s0 = 0; // variables holding zero to avoid
171 uint64_t u0 = 0; // overloading ambiguities below
Vikram S. Advea21cf202001-07-21 12:42:19 +0000172
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. Adve4f231662001-07-28 04:15:15 +0000187 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. Advea21cf202001-07-21 12:42:19 +0000192 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. Adve4f231662001-07-28 04:15:15 +0000199 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000200 break;
201
202 case 5: // stmt: BrUncond
203 mvec[0] = new MachineInstr(BA);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000204 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
205 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000206 ((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. Adve4f231662001-07-28 04:15:15 +0000220 bool isValidConst;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000221
222 if (constVal->getType()->isIntegral()
Vikram S. Adve4f231662001-07-28 04:15:15 +0000223 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
224 && isValidConst)
Vikram S. Advea21cf202001-07-21 12:42:19 +0000225 {
Vikram S. Adve4f231662001-07-28 04:15:15 +0000226 // That constant ia a zero after all...
Vikram S. Advea21cf202001-07-21 12:42:19 +0000227 // Use the left child of the setCC instruction as the first argument!
228 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000229 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000230 subtreeRoot->leftChild()->leftChild()->getValue());
231 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
232 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
233
Vikram S. Adve4f231662001-07-28 04:15:15 +0000234 // delay slot
235 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000236
Vikram S. Adve4f231662001-07-28 04:15:15 +0000237 // 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. Advea21cf202001-07-21 12:42:19 +0000242 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. Adve4f231662001-07-28 04:15:15 +0000250 // and check the branching condition in order to choose the branch to use.
Vikram S. Advea21cf202001-07-21 12:42:19 +0000251 //
252 {
253 bool isFPBranch;
254 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000255 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
256 subtreeRoot->leftChild()->getValue());
257 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000258 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
259
Vikram S. Adve4f231662001-07-28 04:15:15 +0000260 // delay slot
261 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000262
Vikram S. Adve4f231662001-07-28 04:15:15 +0000263 // 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. Advea21cf202001-07-21 12:42:19 +0000269 break;
270 }
Vikram S. Adve4f231662001-07-28 04:15:15 +0000271
Vikram S. Advea21cf202001-07-21 12:42:19 +0000272 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. Adve4f231662001-07-28 04:15:15 +0000277 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
278 subtreeRoot->leftChild()->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +0000279 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. Adve4f231662001-07-28 04:15:15 +0000295 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000296 subtreeRoot->leftChild()->getValue());
297 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000298 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000299 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. Adve4f231662001-07-28 04:15:15 +0000306 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000307 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. Adve74f4a132001-07-31 21:46:57 +0000314 assert(opType->isIntegral() ||
315 opType->isPointerType() ||
316 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
Vikram S. Advea21cf202001-07-21 12:42:19 +0000317 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +0000318 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000319 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. Adve4f231662001-07-28 04:15:15 +0000327 {
328 numInstr = 0;
329 forwardOperandNum = 0;
330 }
Vikram S. Advea21cf202001-07-21 12:42:19 +0000331 else
332 {
333 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000334 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000335 }
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. Adve4f231662001-07-28 04:15:15 +0000351 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000352 }
353 else
354 {
355 opType = subtreeRoot->leftChild()->getValue()->getType();
356 mvec[0] = new MachineInstr(ChooseConvertToFloatInstr(subtreeRoot, opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000357 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000358 }
359 break;
360
361 case 19: // reg: ToArrayTy(reg):
362 case 20: // reg: ToPointerTy(reg):
363 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +0000364 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000365 break;
366
367 case 33: // reg: Add(reg, reg)
368 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000369 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000370 break;
371
372 case 34: // reg: Sub(reg, reg)
373 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000374 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000375 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. Adve4f231662001-07-28 04:15:15 +0000383 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000384 break;
385
386 case 36: // reg: Div(reg, reg)
387 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000388 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000389 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. Adve4f231662001-07-28 04:15:15 +0000397 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000398 break;
399
400 case 138: // reg: And(reg, not)
401 mvec[0] = new MachineInstr(ANDN);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000402 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000403 break;
404
405 case 39: // reg: Or(reg, reg)
406 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000407 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000408 break;
409
410 case 139: // reg: Or(reg, not)
411 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000412 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000413 break;
414
415 case 40: // reg: Xor(reg, reg)
416 mvec[0] = new MachineInstr(XOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000417 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000418 break;
419
420 case 140: // reg: Xor(reg, not)
421 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000422 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000423 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. Adve4f231662001-07-28 04:15:15 +0000434 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Advea21cf202001-07-21 12:42:19 +0000435 //
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. Adve4f231662001-07-28 04:15:15 +0000444 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
Vikram S. Advea21cf202001-07-21 12:42:19 +0000445 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
446 parentOpCode <= BRGEZ)
447 {
Vikram S. Adve4f231662001-07-28 04:15:15 +0000448 numInstr = 0; // don't forward the operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +0000449 break;
450 }
451 }
452 // ELSE FALL THROUGH
453
454 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4f231662001-07-28 04:15:15 +0000455 {
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. Advea21cf202001-07-21 12:42:19 +0000472 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral())
473 {
Vikram S. Adve4f231662001-07-28 04:15:15 +0000474 // 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. Advea21cf202001-07-21 12:42:19 +0000479 mvec[0] = new MachineInstr(SUBcc);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000480 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult);
481
Vikram S. Adve98a9c972001-08-06 21:06:10 +0000482 // mark the 4th operand as being a CC register, and a "result"
483 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
484 subtreeRoot->getValue(), /*def*/ true);
485
486 if (!discardResult)
Vikram S. Adve4f231662001-07-28 04:15:15 +0000487 { // recompute bool if needed, using the integer condition codes
488 if (result->getOpcode() == Instruction::SetNE)
489 discardResult = true;
490 else
491 movOpCode =
492 ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove);
493 }
Vikram S. Advea21cf202001-07-21 12:42:19 +0000494 }
495 else
496 {
Vikram S. Adve4f231662001-07-28 04:15:15 +0000497 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Advea21cf202001-07-21 12:42:19 +0000498 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Advea21cf202001-07-21 12:42:19 +0000499 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4f231662001-07-28 04:15:15 +0000500 subtreeRoot->getValue());
501 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
502 subtreeRoot->leftChild()->getValue());
503 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
504 subtreeRoot->rightChild()->getValue());
505
506 if (!discardResult)
507 {// recompute bool using the FP condition codes
508 mustClearReg = true;
509 valueToMove = 1;
510 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
511 }
512 }
513
514 if (!discardResult)
515 {
516 if (mustClearReg)
517 {// Unconditionally set register to 0
518 int n = numInstr++;
519 mvec[n] = new MachineInstr(SETHI);
520 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0);
521 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
522 subtreeRoot->getValue());
523 }
524
525 // Now conditionally move `valueToMove' (0 or 1) into the register
526 int n = numInstr++;
527 mvec[n] = new MachineInstr(movOpCode);
528 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
529 subtreeRoot->getValue());
530 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
531 valueToMove);
532 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
533 subtreeRoot->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +0000534 }
535 break;
Vikram S. Adve4f231662001-07-28 04:15:15 +0000536 }
537
Vikram S. Advea21cf202001-07-21 12:42:19 +0000538 case 43: // boolreg: VReg
Vikram S. Adve74f4a132001-07-31 21:46:57 +0000539 case 44: // boolreg: Constant
Vikram S. Advea21cf202001-07-21 12:42:19 +0000540 numInstr = 0;
541 break;
542
543 case 51: // reg: Load(reg)
544 case 52: // reg: Load(ptrreg)
545 case 53: // reg: LoadIdx(reg,reg)
546 case 54: // reg: LoadIdx(ptrreg,reg)
547 mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000548 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000549 break;
550
551 case 55: // reg: GetElemPtr(reg)
552 case 56: // reg: GetElemPtrIdx(reg,reg)
553 if (subtreeRoot->parent() != NULL)
554 {
555 // Check if the parent was an array access.
556 // If so, we still need to generate this instruction.
557 MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction();
558 const PointerType* ptrType =
559 (const PointerType*) memInst->getPtrOperand()->getType();
560 if (! ptrType->getValueType()->isArrayType())
561 {// we don't need a separate instr
Vikram S. Adve4f231662001-07-28 04:15:15 +0000562 numInstr = 0; // don't forward operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +0000563 break;
564 }
565 }
566 // else in all other cases we need to a separate ADD instruction
567 mvec[0] = new MachineInstr(ADD);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000568 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000569 break;
570
571 case 57: // reg: Alloca: Implement as 2 instructions:
572 // sub %sp, tmp -> %sp
573 { // add %sp, 0 -> result
574 Instruction* instr = subtreeRoot->getInstruction();
575 const PointerType* instrType = (const PointerType*) instr->getType();
576 assert(instrType->isPointerType());
Vikram S. Adve4f231662001-07-28 04:15:15 +0000577 int tsize = (int) target.findOptimalStorageSize(instrType->getValueType());
578 assert(tsize != 0 && "Just to check when this can happen");
579 // if (tsize == 0)
580 // {
581 // numInstr = 0;
582 // break;
583 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +0000584 //else go on to create the instructions needed...
585
586 // Create a temporary Value to hold the constant type-size
587 ConstPoolSInt* valueForTSize = new ConstPoolSInt(Type::IntTy, tsize);
588 ConstantPool &cpool = instr->getParent()->getParent()->getConstantPool();
589 if (cpool.find(valueForTSize) == 0)
590 cpool.insert(valueForTSize);
591
592 // Instruction 1: sub %sp, tsize -> %sp
593 // tsize is always constant, but it may have to be put into a
594 // register if it doesn't fit in the immediate field.
595 //
596 mvec[0] = new MachineInstr(SUB);
597 mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000598 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000599 mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
600
601 // Instruction 2: add %sp, 0 -> result
602 numInstr++;
603 mvec[1] = new MachineInstr(ADD);
604 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
605 mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000606 mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000607 break;
608 }
609
610 case 58: // reg: Alloca(reg): Implement as 3 instructions:
611 // mul num, typeSz -> tmp
612 // sub %sp, tmp -> %sp
613 { // add %sp, 0 -> result
614 Instruction* instr = subtreeRoot->getInstruction();
615 const PointerType* instrType = (const PointerType*) instr->getType();
616 assert(instrType->isPointerType() &&
617 instrType->getValueType()->isArrayType());
618 const Type* eltType =
619 ((ArrayType*) instrType->getValueType())->getElementType();
Vikram S. Adve4f231662001-07-28 04:15:15 +0000620 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000621
Vikram S. Adve4f231662001-07-28 04:15:15 +0000622 assert(tsize != 0 && "Just to check when this can happen");
623 // if (tsize == 0)
624 // {
625 // numInstr = 0;
626 // break;
627 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +0000628 //else go on to create the instructions needed...
629
630 // Create a temporary Value to hold the constant type-size
631 ConstPoolSInt* valueForTSize = new ConstPoolSInt(Type::IntTy, tsize);
632 ConstantPool &cpool = instr->getParent()->getParent()->getConstantPool();
633 if (cpool.find(valueForTSize) == 0)
634 cpool.insert(valueForTSize);
635
636 // Create a temporary value to hold `tmp'
637 Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1,
638 subtreeRoot->leftChild()->getValue(),
639 NULL /*could insert tsize here*/);
640 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
641
642 // Instruction 1: mul numElements, typeSize -> tmp
643 mvec[0] = new MachineInstr(MULX);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000644 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000645 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4f231662001-07-28 04:15:15 +0000646 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
647 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000648
Vikram S. Adve4f231662001-07-28 04:15:15 +0000649 tmpInstr->addMachineInstruction(mvec[0]);
650
Vikram S. Advea21cf202001-07-21 12:42:19 +0000651 // Instruction 2: sub %sp, tmp -> %sp
652 numInstr++;
653 mvec[1] = new MachineInstr(SUB);
654 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000655 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000656 mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
657
658 // Instruction 3: add %sp, 0 -> result
659 numInstr++;
660 mvec[2] = new MachineInstr(ADD);
661 mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
662 mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000663 mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000664 break;
665 }
666
667 case 61: // reg: Call
668 // Generate a call-indirect (i.e., JMPL) for now to expose
669 // the potential need for registers. If an absolute address
670 // is available, replace this with a CALL instruction.
671 // Mark both the indirection register and the return-address
672 { // register as hidden virtual registers.
673
Vikram S. Adve4f231662001-07-28 04:15:15 +0000674 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000675 ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000676 Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1,
677 subtreeRoot->getValue(), NULL);
678 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg);
679 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000680
681 mvec[0] = new MachineInstr(JMPL);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000682 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000683 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
684 (int64_t) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000685 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg);
686
687 // NOTE: jmpAddrReg will be loaded by a different instruction generated
688 // by the final code generator, so we just mark the CALL instruction
689 // as computing that value.
690 // The retAddrReg is actually computed by the CALL instruction.
691 //
692 jmpAddrReg->addMachineInstruction(mvec[0]);
693 retAddrReg->addMachineInstruction(mvec[0]);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000694
695 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
696 break;
697 }
698
699 case 62: // reg: Shl(reg, reg)
700 opType = subtreeRoot->leftChild()->getValue()->getType();
701 assert(opType->isIntegral() || opType == Type::BoolTy);
702 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000703 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000704 break;
705
706 case 63: // reg: Shr(reg, reg)
707 opType = subtreeRoot->leftChild()->getValue()->getType();
708 assert(opType->isIntegral() || opType == Type::BoolTy);
709 mvec[0] = new MachineInstr((opType->isSigned()
710 ? ((opType == Type::LongTy)? SRAX : SRA)
711 : ((opType == Type::LongTy)? SRLX : SRL)));
Vikram S. Adve4f231662001-07-28 04:15:15 +0000712 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000713 break;
714
Vikram S. Adve74f4a132001-07-31 21:46:57 +0000715 case 64: // reg: Phi(reg,reg)
716 { // This instruction has variable #operands, so resultPos is 0.
717 Instruction* phi = subtreeRoot->getInstruction();
718 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
719 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Ruchira Sasanka81cb2072001-08-13 16:24:01 +0000720 subtreeRoot->getValue(), true);
Vikram S. Adve74f4a132001-07-31 21:46:57 +0000721 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
722 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
723 phi->getOperand(i));
724 break;
725 }
Vikram S. Advea21cf202001-07-21 12:42:19 +0000726 case 71: // reg: VReg
727 case 72: // reg: Constant
Vikram S. Adve4f231662001-07-28 04:15:15 +0000728 numInstr = 0; // don't forward the value
Vikram S. Advea21cf202001-07-21 12:42:19 +0000729 break;
730
731 case 111: // stmt: reg
732 case 112: // stmt: boolconst
733 case 113: // stmt: bool
734 case 121:
735 case 122:
736 case 123:
737 case 124:
738 case 125:
739 case 126:
740 case 127:
741 case 128:
742 case 129:
743 case 130:
744 case 131:
745 case 132:
746 case 153:
Vikram S. Adve74f4a132001-07-31 21:46:57 +0000747 case 155:
Vikram S. Advea21cf202001-07-21 12:42:19 +0000748 //
749 // These are all chain rules, which have a single nonterminal on the RHS.
750 // Get the rule that matches the RHS non-terminal and use that instead.
751 //
752 assert(ThisIsAChainRule(ruleForNode));
753 assert(nts[0] && ! nts[1]
754 && "A chain rule should have only one RHS non-terminal!");
755 nextRule = burm_rule(subtreeRoot->getBasicNode()->state, nts[0]);
756 nts = burm_nts[nextRule];
Vikram S. Adve4f231662001-07-28 04:15:15 +0000757 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000758 break;
759
760 default:
Vikram S. Adve4f231662001-07-28 04:15:15 +0000761 assert(0 && "Unrecognized BURG rule");
Vikram S. Advea21cf202001-07-21 12:42:19 +0000762 numInstr = 0;
763 break;
764 }
765
Vikram S. Adve4f231662001-07-28 04:15:15 +0000766 if (forwardOperandNum >= 0)
767 { // We did not generate a machine instruction but need to use operand.
768 // If user is in the same tree, replace Value in its machine operand.
769 // If not, insert a copy instruction which should get coalesced away
770 // by register allocation.
771 if (subtreeRoot->parent() != NULL)
772 ForwardOperand(subtreeRoot, (InstructionNode*) subtreeRoot->parent(),
773 forwardOperandNum);
774 else
775 {
776 int n = numInstr++;
777 mvec[n] = new MachineInstr(ADD);
778 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
779 subtreeRoot->getInstruction()->getOperand(forwardOperandNum));
780 mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
781 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
782 subtreeRoot->getInstruction());
783 }
784 }
785
786 if (! ThisIsAChainRule(ruleForNode))
787 numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000788
789 return numInstr;
790}
791
792
793//---------------------------------------------------------------------------
794// Private helper routines for SPARC instruction selection.
795//---------------------------------------------------------------------------
796
797
798static MachineOpCode
799ChooseBprInstruction(const InstructionNode* instrNode)
800{
801 MachineOpCode opCode;
802
803 Instruction* setCCInstr =
804 ((InstructionNode*) instrNode->leftChild())->getInstruction();
805
806 switch(setCCInstr->getOpcode())
807 {
808 case Instruction::SetEQ: opCode = BRZ; break;
809 case Instruction::SetNE: opCode = BRNZ; break;
810 case Instruction::SetLE: opCode = BRLEZ; break;
811 case Instruction::SetGE: opCode = BRGEZ; break;
812 case Instruction::SetLT: opCode = BRLZ; break;
813 case Instruction::SetGT: opCode = BRGZ; break;
814 default:
815 assert(0 && "Unrecognized VM instruction!");
816 opCode = INVALID_OPCODE;
817 break;
818 }
819
820 return opCode;
821}
822
823
824static MachineOpCode
825ChooseBccInstruction(const InstructionNode* instrNode,
826 bool& isFPBranch)
827{
828 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
829 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
830 const Type* setCCType = setCCInstr->getOperand(0)->getType();
831
832 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
833
834 if (isFPBranch)
Vikram S. Adve4f231662001-07-28 04:15:15 +0000835 return ChooseBFpccInstruction(instrNode, setCCInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000836 else
837 return ChooseBpccInstruction(instrNode, setCCInstr);
838}
839
840
841static MachineOpCode
842ChooseBpccInstruction(const InstructionNode* instrNode,
843 const BinaryOperator* setCCInstr)
844{
845 MachineOpCode opCode = INVALID_OPCODE;
846
847 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
848
849 if (isSigned)
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 = BLE; break;
856 case Instruction::SetGE: opCode = BGE; break;
857 case Instruction::SetLT: opCode = BL; break;
858 case Instruction::SetGT: opCode = BG; break;
859 default:
860 assert(0 && "Unrecognized VM instruction!");
861 break;
862 }
863 }
864 else
865 {
866 switch(setCCInstr->getOpcode())
867 {
868 case Instruction::SetEQ: opCode = BE; break;
869 case Instruction::SetNE: opCode = BNE; break;
870 case Instruction::SetLE: opCode = BLEU; break;
871 case Instruction::SetGE: opCode = BCC; break;
872 case Instruction::SetLT: opCode = BCS; break;
873 case Instruction::SetGT: opCode = BGU; break;
874 default:
875 assert(0 && "Unrecognized VM instruction!");
876 break;
877 }
878 }
879
880 return opCode;
881}
882
883static MachineOpCode
Vikram S. Adve4f231662001-07-28 04:15:15 +0000884ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Advea21cf202001-07-21 12:42:19 +0000885 const BinaryOperator* setCCInstr)
886{
887 MachineOpCode opCode = INVALID_OPCODE;
888
889 switch(setCCInstr->getOpcode())
890 {
891 case Instruction::SetEQ: opCode = FBE; break;
892 case Instruction::SetNE: opCode = FBNE; break;
893 case Instruction::SetLE: opCode = FBLE; break;
894 case Instruction::SetGE: opCode = FBGE; break;
895 case Instruction::SetLT: opCode = FBL; break;
896 case Instruction::SetGT: opCode = FBG; break;
897 default:
898 assert(0 && "Unrecognized VM instruction!");
899 break;
900 }
901
902 return opCode;
903}
904
Vikram S. Adve4f231662001-07-28 04:15:15 +0000905
Vikram S. Advea21cf202001-07-21 12:42:19 +0000906static MachineOpCode
Vikram S. Adve4f231662001-07-28 04:15:15 +0000907ChooseMovFpccInstruction(const InstructionNode* instrNode)
908{
909 MachineOpCode opCode = INVALID_OPCODE;
910
911 switch(instrNode->getInstruction()->getOpcode())
912 {
913 case Instruction::SetEQ: opCode = MOVFE; break;
914 case Instruction::SetNE: opCode = MOVFNE; break;
915 case Instruction::SetLE: opCode = MOVFLE; break;
916 case Instruction::SetGE: opCode = MOVFGE; break;
917 case Instruction::SetLT: opCode = MOVFL; break;
918 case Instruction::SetGT: opCode = MOVFG; break;
919 default:
920 assert(0 && "Unrecognized VM instruction!");
921 break;
922 }
923
924 return opCode;
925}
926
927
928// Assumes that SUBcc v1, v2 -> v3 has been executed.
929// In most cases, we want to clear v3 and then follow it by instruction
930// MOVcc 1 -> v3.
931// Set mustClearReg=false if v3 need not be cleared before conditional move.
932// Set valueToMove=0 if we want to conditionally move 0 instead of 1
933// (i.e., we want to test inverse of a condition)
934//
935//
936static MachineOpCode
937ChooseMovpccAfterSub(const InstructionNode* instrNode,
938 bool& mustClearReg,
939 int& valueToMove)
940{
941 MachineOpCode opCode = INVALID_OPCODE;
942 mustClearReg = true;
943 valueToMove = 1;
944
945 switch(instrNode->getInstruction()->getOpcode())
946 {
947 case Instruction::SetEQ: opCode = MOVNE; mustClearReg = false;
948 valueToMove = 0; break;
949 case Instruction::SetLE: opCode = MOVLE; break;
950 case Instruction::SetGE: opCode = MOVGE; break;
951 case Instruction::SetLT: opCode = MOVL; break;
952 case Instruction::SetGT: opCode = MOVG; break;
953
954 case Instruction::SetNE: assert(0 && "No move required!");
955
956 default:
957 assert(0 && "Unrecognized VM instruction!");
958 break;
959 }
960
961 return opCode;
962}
963
964
965static MachineOpCode
Vikram S. Advea21cf202001-07-21 12:42:19 +0000966ChooseConvertToFloatInstr(const InstructionNode* instrNode,
967 const Type* opType)
968{
969 MachineOpCode opCode = INVALID_OPCODE;
970
971 switch(instrNode->getOpLabel())
972 {
973 case ToFloatTy:
974 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
975 opCode = FITOS;
976 else if (opType == Type::LongTy)
977 opCode = FXTOS;
978 else if (opType == Type::DoubleTy)
979 opCode = FDTOS;
980 else
981 assert(0 && "Cannot convert this type to FLOAT on SPARC");
982 break;
983
984 case ToDoubleTy:
985 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
986 opCode = FITOD;
987 else if (opType == Type::LongTy)
988 opCode = FXTOD;
989 else if (opType == Type::FloatTy)
990 opCode = FSTOD;
991 else
992 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
993 break;
994
995 default:
996 break;
997 }
998
999 return opCode;
1000}
1001
1002static MachineOpCode
1003ChooseConvertToIntInstr(const InstructionNode* instrNode,
1004 const Type* opType)
1005{
1006 MachineOpCode opCode = INVALID_OPCODE;;
1007
1008 int instrType = (int) instrNode->getOpLabel();
1009
1010 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
1011 {
1012 switch (opType->getPrimitiveID())
1013 {
1014 case Type::FloatTyID: opCode = FSTOI; break;
1015 case Type::DoubleTyID: opCode = FDTOI; break;
1016 default:
1017 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
1018 break;
1019 }
1020 }
1021 else if (instrType == ToLongTy)
1022 {
1023 switch (opType->getPrimitiveID())
1024 {
1025 case Type::FloatTyID: opCode = FSTOX; break;
1026 case Type::DoubleTyID: opCode = FDTOX; break;
1027 default:
1028 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
1029 break;
1030 }
1031 }
1032 else
1033 assert(0 && "Should not get here, Mo!");
1034
1035 return opCode;
1036}
1037
1038
1039static MachineOpCode
1040ChooseAddInstruction(const InstructionNode* instrNode)
1041{
1042 MachineOpCode opCode = INVALID_OPCODE;
1043
1044 const Type* resultType = instrNode->getInstruction()->getType();
1045
1046 if (resultType->isIntegral() ||
1047 resultType->isPointerType() ||
1048 resultType->isMethodType() ||
1049 resultType->isLabelType())
1050 {
1051 opCode = ADD;
1052 }
1053 else
1054 {
1055 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
1056 switch(operand->getType()->getPrimitiveID())
1057 {
1058 case Type::FloatTyID: opCode = FADDS; break;
1059 case Type::DoubleTyID: opCode = FADDD; break;
1060 default: assert(0 && "Invalid type for ADD instruction"); break;
1061 }
1062 }
1063
1064 return opCode;
1065}
1066
1067static MachineOpCode
1068ChooseSubInstruction(const InstructionNode* instrNode)
1069{
1070 MachineOpCode opCode = INVALID_OPCODE;
1071
1072 const Type* resultType = instrNode->getInstruction()->getType();
1073
1074 if (resultType->isIntegral() ||
1075 resultType->isPointerType())
1076 {
1077 opCode = SUB;
1078 }
1079 else
1080 {
1081 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
1082 switch(operand->getType()->getPrimitiveID())
1083 {
1084 case Type::FloatTyID: opCode = FSUBS; break;
1085 case Type::DoubleTyID: opCode = FSUBD; break;
1086 default: assert(0 && "Invalid type for SUB instruction"); break;
1087 }
1088 }
1089
1090 return opCode;
1091}
1092
1093
1094static MachineOpCode
1095ChooseFcmpInstruction(const InstructionNode* instrNode)
1096{
1097 MachineOpCode opCode = INVALID_OPCODE;
1098
1099 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
1100 switch(operand->getType()->getPrimitiveID())
1101 {
1102 case Type::FloatTyID: opCode = FCMPS; break;
1103 case Type::DoubleTyID: opCode = FCMPD; break;
1104 default: assert(0 && "Invalid type for ADD instruction"); break;
1105 }
1106
1107 return opCode;
1108}
1109
1110
1111static MachineOpCode
1112ChooseMulInstruction(const InstructionNode* instrNode,
1113 bool checkCasts)
1114{
1115 MachineOpCode opCode = INVALID_OPCODE;
1116
1117 if (checkCasts)
1118 {
1119 // Assume that leftArg and rightArg are both cast instructions.
1120 //
1121 InstrTreeNode* leftArg = instrNode->leftChild();
1122 InstrTreeNode* rightArg = instrNode->rightChild();
1123 InstrTreeNode* leftArgArg = leftArg->leftChild();
1124 InstrTreeNode* rightArgArg = rightArg->leftChild();
1125 assert(leftArg->getValue()->getType() ==rightArg->getValue()->getType());
1126
1127 // If both arguments are floats cast to double, use FsMULd
1128 if (leftArg->getValue()->getType() == Type::DoubleTy &&
1129 leftArgArg->getValue()->getType() == Type::FloatTy &&
1130 rightArgArg->getValue()->getType() == Type::FloatTy)
1131 {
1132 return opCode = FSMULD;
1133 }
1134 // else fall through and use the regular multiply instructions
1135 }
1136
1137 const Type* resultType = instrNode->getInstruction()->getType();
1138
1139 if (resultType->isIntegral())
1140 {
1141 opCode = MULX;
1142 }
1143 else
1144 {
1145 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
1146 {
1147 case Type::FloatTyID: opCode = FMULS; break;
1148 case Type::DoubleTyID: opCode = FMULD; break;
1149 default: assert(0 && "Invalid type for MUL instruction"); break;
1150 }
1151 }
1152
1153 return opCode;
1154}
1155
1156
1157static MachineOpCode
1158ChooseDivInstruction(const InstructionNode* instrNode)
1159{
1160 MachineOpCode opCode = INVALID_OPCODE;
1161
1162 const Type* resultType = instrNode->getInstruction()->getType();
1163
1164 if (resultType->isIntegral())
1165 {
1166 opCode = resultType->isSigned()? SDIVX : UDIVX;
1167 }
1168 else
1169 {
1170 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
1171 switch(operand->getType()->getPrimitiveID())
1172 {
1173 case Type::FloatTyID: opCode = FDIVS; break;
1174 case Type::DoubleTyID: opCode = FDIVD; break;
1175 default: assert(0 && "Invalid type for DIV instruction"); break;
1176 }
1177 }
1178
1179 return opCode;
1180}
1181
1182
1183static MachineOpCode
1184ChooseLoadInstruction(const Type* resultType)
1185{
1186 MachineOpCode opCode = INVALID_OPCODE;
1187
1188 switch (resultType->getPrimitiveID())
1189 {
1190 case Type::BoolTyID: opCode = LDUB; break;
1191 case Type::UByteTyID: opCode = LDUB; break;
1192 case Type::SByteTyID: opCode = LDSB; break;
1193 case Type::UShortTyID: opCode = LDUH; break;
1194 case Type::ShortTyID: opCode = LDSH; break;
1195 case Type::UIntTyID: opCode = LDUW; break;
1196 case Type::IntTyID: opCode = LDSW; break;
1197 case Type::ULongTyID:
1198 case Type::LongTyID: opCode = LDX; break;
1199 case Type::FloatTyID: opCode = LD; break;
1200 case Type::DoubleTyID: opCode = LDD; break;
1201 default: assert(0 && "Invalid type for Load instruction"); break;
1202 }
1203
1204 return opCode;
1205}
1206
1207
1208static MachineOpCode
1209ChooseStoreInstruction(const Type* valueType)
1210{
1211 MachineOpCode opCode = INVALID_OPCODE;
1212
1213 switch (valueType->getPrimitiveID())
1214 {
1215 case Type::BoolTyID:
1216 case Type::UByteTyID:
1217 case Type::SByteTyID: opCode = STB; break;
1218 case Type::UShortTyID:
1219 case Type::ShortTyID: opCode = STH; break;
1220 case Type::UIntTyID:
1221 case Type::IntTyID: opCode = STW; break;
1222 case Type::ULongTyID:
1223 case Type::LongTyID: opCode = STX; break;
1224 case Type::FloatTyID: opCode = ST; break;
1225 case Type::DoubleTyID: opCode = STD; break;
1226 default: assert(0 && "Invalid type for Store instruction"); break;
1227 }
1228
1229 return opCode;
1230}
1231
1232
1233//------------------------------------------------------------------------
1234// Function SetOperandsForMemInstr
1235//
1236// Choose addressing mode for the given load or store instruction.
1237// Use [reg+reg] if it is an indexed reference, and the index offset is
1238// not a constant or if it cannot fit in the offset field.
1239// Use [reg+offset] in all other cases.
1240//
1241// This assumes that all array refs are "lowered" to one of these forms:
1242// %x = load (subarray*) ptr, constant ; single constant offset
1243// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1244// Generally, this should happen via strength reduction + LICM.
1245// Also, strength reduction should take care of using the same register for
1246// the loop index variable and an array index, when that is profitable.
1247//------------------------------------------------------------------------
1248
1249static void
1250SetOperandsForMemInstr(MachineInstr* minstr,
1251 const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001252 const TargetMachine& target)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001253{
1254 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1255
1256 // Variables to hold the index vector, ptr value, and offset value.
1257 // The major work here is to extract these for all 3 instruction types
1258 // and then call the common function SetMemOperands_Internal().
1259 //
1260 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
1261 vector<ConstPoolVal*>* newIdxVec = NULL;
1262 Value* ptrVal;
1263 Value* arrayOffsetVal = NULL;
1264
1265 // Test if a GetElemPtr instruction is being folded into this mem instrn.
1266 // If so, it will be in the left child for Load and GetElemPtr,
1267 // and in the right child for Store instructions.
1268 //
1269 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
1270 ? vmInstrNode->rightChild()
1271 : vmInstrNode->leftChild());
1272
1273 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
1274 ptrChild->getOpLabel() == GetElemPtrIdx)
1275 {
1276 // There is a GetElemPtr instruction and there may be a chain of
1277 // more than one. Use the pointer value of the last one in the chain.
1278 // Fold the index vectors from the entire chain and from the mem
1279 // instruction into one single index vector.
1280 // Finally, we never fold for an array instruction so make that NULL.
1281
1282 newIdxVec = new vector<ConstPoolVal*>;
1283 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
1284
1285 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
1286 idxVec = newIdxVec;
1287
1288 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
1289 && "GetElemPtr cannot be folded into array refs in selection");
1290 }
1291 else
1292 {
1293 // There is no GetElemPtr instruction.
1294 // Use the pointer value and the index vector from the Mem instruction.
1295 // If it is an array reference, get the array offset value.
1296 //
1297 ptrVal = memInst->getPtrOperand();
1298
1299 const Type* opType =
1300 ((const PointerType*) ptrVal->getType())->getValueType();
1301 if (opType->isArrayType())
1302 {
1303 assert((memInst->getNumOperands()
1304 == (unsigned) 1 + memInst->getFirstOffsetIdx())
1305 && "Array refs must be lowered before Instruction Selection");
1306
1307 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
1308 }
1309 }
1310
1311 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001312 *idxVec, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001313
1314 if (newIdxVec != NULL)
1315 delete newIdxVec;
1316}
1317
1318
1319static void
1320SetMemOperands_Internal(MachineInstr* minstr,
1321 const InstructionNode* vmInstrNode,
1322 Value* ptrVal,
1323 Value* arrayOffsetVal,
1324 const vector<ConstPoolVal*>& idxVec,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001325 const TargetMachine& target)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001326{
1327 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1328
1329 // Initialize so we default to storing the offset in a register.
1330 int64_t smallConstOffset;
1331 Value* valueForRegOffset = NULL;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001332 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001333
1334 // Check if there is an index vector and if so, if it translates to
1335 // a small enough constant to fit in the immediate-offset field.
1336 //
1337 if (idxVec.size() > 0)
1338 {
1339 bool isConstantOffset = false;
1340 unsigned offset;
1341
1342 const PointerType* ptrType = (PointerType*) ptrVal->getType();
1343
1344 if (ptrType->getValueType()->isStructType())
1345 {
1346 // the offset is always constant for structs
1347 isConstantOffset = true;
1348
1349 // Compute the offset value using the index vector
1350 offset = MemAccessInst::getIndexedOfsetForTarget(ptrType,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001351 idxVec, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001352 }
1353 else
1354 {
1355 // It must be an array ref. Check if the offset is a constant,
1356 // and that the indexing has been lowered to a single offset.
1357 //
1358 assert(ptrType->getValueType()->isArrayType());
1359 assert(arrayOffsetVal != NULL
1360 && "Expect to be given Value* for array offsets");
1361
1362 if (arrayOffsetVal->getValueType() == Value::ConstantVal)
1363 {
1364 isConstantOffset = true; // always constant for structs
1365 assert(arrayOffsetVal->getType()->isIntegral());
1366 offset = (arrayOffsetVal->getType()->isSigned())
1367 ? ((ConstPoolSInt*) arrayOffsetVal)->getValue()
1368 : (int64_t) ((ConstPoolUInt*) arrayOffsetVal)->getValue();
1369 }
1370 else
1371 {
1372 valueForRegOffset = arrayOffsetVal;
1373 }
1374 }
1375
1376 if (isConstantOffset)
1377 {
1378 // create a virtual register for the constant
1379 valueForRegOffset = new ConstPoolSInt(Type::IntTy, offset);
1380 }
1381 }
1382 else
1383 {
1384 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1385 smallConstOffset = 0;
1386 }
1387
1388 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1389 // It is the left child in the instruction tree in all cases.
1390 Value* leftVal = vmInstrNode->leftChild()->getValue();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001391 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001392
1393 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1394 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1395 //
1396 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001397 if (offsetOpType == MachineOperand::MO_VirtualRegister)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001398 {
1399 assert(valueForRegOffset != NULL);
1400 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1401 }
1402 else
1403 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1404
1405 if (memInst->getOpcode() == Instruction::Store)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001406 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001407 else
Vikram S. Adve4f231662001-07-28 04:15:15 +00001408 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001409 vmInstrNode->getValue());
1410}
1411
1412
Vikram S. Adve4f231662001-07-28 04:15:15 +00001413// Special handling for constant operands:
1414// -- if the constant is 0, use the hardwired 0 register, if any
1415// -- if the constant fits in the IMMEDIATE field, use that field
1416// -- else insert instructions to put the constant into a register, either
1417// directly or by loading explicitly from the constant pool.
1418//
Vikram S. Advea21cf202001-07-21 12:42:19 +00001419static unsigned
1420FixConstantOperands(const InstructionNode* vmInstrNode,
1421 MachineInstr** mvec,
1422 unsigned numInstr,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001423 TargetMachine& target)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001424{
1425 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1426
1427 unsigned numNew = 0;
1428 Instruction* vmInstr = vmInstrNode->getInstruction();
1429
1430 for (unsigned i=0; i < numInstr; i++)
1431 {
1432 MachineInstr* minstr = mvec[i];
Vikram S. Adve4f231662001-07-28 04:15:15 +00001433 const MachineInstrDescriptor& instrDesc =
1434 target.getInstrInfo().getDescriptor(minstr->getOpCode());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001435
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001436 for (unsigned op=0; op < minstr->getNumOperands(); op++)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001437 {
1438 const MachineOperand& mop = minstr->getOperand(op);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001439
Vikram S. Adve4f231662001-07-28 04:15:15 +00001440 // skip the result position (for efficiency below) and any other
1441 // positions already marked as not a virtual register
1442 if (instrDesc.resultPos == (int) op ||
1443 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1444 mop.getVRegValue() == NULL)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001445 {
1446 break;
1447 }
1448
Vikram S. Adve4f231662001-07-28 04:15:15 +00001449 Value* opValue = mop.getVRegValue();
1450
Vikram S. Advea21cf202001-07-21 12:42:19 +00001451 if (opValue->getValueType() == Value::ConstantVal)
1452 {
Vikram S. Advea21cf202001-07-21 12:42:19 +00001453 unsigned int machineRegNum;
1454 int64_t immedValue;
1455 MachineOperand::MachineOperandType opType =
Vikram S. Adve4f231662001-07-28 04:15:15 +00001456 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001457 /*canUseImmed*/ (op == 1),
Vikram S. Adve4f231662001-07-28 04:15:15 +00001458 machineRegNum, immedValue);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001459
Vikram S. Adve4f231662001-07-28 04:15:15 +00001460 if (opType == MachineOperand::MO_MachineRegister)
1461 minstr->SetMachineOperand(op, machineRegNum);
1462 else if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001463 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001464 // value is constant and must be loaded into a register
1465 TmpInstruction* tmpReg;
1466 loadConstVec[numNew++] =
1467 MakeOneLoadConstInstr(vmInstr, opValue, tmpReg);
1468 minstr->SetMachineOperand(op, opType, tmpReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001469 }
1470 else
1471 minstr->SetMachineOperand(op, opType, immedValue);
1472 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001473 }
1474 }
1475
1476 if (numNew > 0)
1477 {
1478 // Insert the new instructions *before* the old ones by moving
1479 // the old ones over `numNew' positions (last-to-first, of course!).
1480 //
1481 for (int i=numInstr-1; i >= ((int) numInstr) - (int) numNew; i--)
1482 mvec[i+numNew] = mvec[i];
1483
1484 for (unsigned i=0; i < numNew; i++)
1485 mvec[i] = loadConstVec[i];
1486 }
1487
1488 return (numInstr + numNew);
1489}
1490
1491
Vikram S. Adve4f231662001-07-28 04:15:15 +00001492#if 0
Vikram S. Advea21cf202001-07-21 12:42:19 +00001493// Create one or two load instructions to load constants from the
1494// constant pool. The first instructions is stored in instrA;
1495// the second (if any) in instrB.
1496//
1497static unsigned
1498InsertLoadConstInstructions(unsigned loadConstFlags,
1499 const InstructionNode* vmInstrNode,
1500 MachineInstr** mvec,
1501 unsigned numInstr)
1502{
1503 MachineInstr *instrA = NULL, *instrB = NULL;
1504
1505 unsigned numNew = 0;
1506
1507 if (loadConstFlags & 0x01)
1508 {
1509 instrA = MakeOneLoadConstInstr(vmInstrNode->getInstruction(),
1510 vmInstrNode->leftChild()->getValue());
1511 numNew++;
1512 }
1513
1514 if (loadConstFlags & 0x02)
1515 {
1516 instrB = MakeOneLoadConstInstr(vmInstrNode->getInstruction(),
1517 vmInstrNode->rightChild()->getValue());
1518 numNew++;
1519 }
1520
1521 // Now insert the new instructions *before* the old ones by
1522 // moving the old ones over `numNew' positions (last-to-first, of course!).
1523 //
1524 for (int i=numInstr-1; i >= ((int) numInstr) - (int) numNew; i--)
1525 mvec[i+numNew] = mvec[i];
1526
1527 unsigned whichNew = 0;
1528 if (instrA != NULL)
1529 mvec[whichNew++] = instrA;
1530 if (instrB != NULL)
1531 mvec[whichNew++] = instrB;
1532 assert(whichNew == numNew);
1533
1534 return numInstr + numNew;
1535}
Vikram S. Adve4f231662001-07-28 04:15:15 +00001536#endif
Vikram S. Advea21cf202001-07-21 12:42:19 +00001537
1538
1539static MachineInstr*
1540MakeOneLoadConstInstr(Instruction* vmInstr,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001541 Value* val,
1542 TmpInstruction*& tmpReg)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001543{
1544 assert(val->getValueType() == Value::ConstantVal);
1545
1546 MachineInstr* minstr;
1547
Vikram S. Adve4f231662001-07-28 04:15:15 +00001548 // Create a TmpInstruction to mark the hidden register used for the constant
1549 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1550 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1551
Vikram S. Advea21cf202001-07-21 12:42:19 +00001552 // Use a "set" instruction for known constants that can go in an integer reg.
1553 // Use a "load" instruction for all other constants, in particular,
1554 // floating point constants.
1555 //
1556 const Type* valType = val->getType();
1557 if (valType->isIntegral() ||
1558 valType->isPointerType() ||
1559 valType == Type::BoolTy)
1560 {
1561 bool isValidConstant;
1562 if (val->getType()->isSigned())
1563 {
1564 minstr = new MachineInstr(SETSW);
1565 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed,
1566 GetSignedIntConstantValue(val, isValidConstant));
1567 }
1568 else
1569 {
1570 minstr = new MachineInstr(SETUW);
1571 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed,
1572 GetUnsignedIntConstantValue(val, isValidConstant));
1573 }
Vikram S. Adve4f231662001-07-28 04:15:15 +00001574 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001575 assert(isValidConstant && "Unrecognized constant");
1576 }
1577 else
1578 {
1579 assert(valType == Type::FloatTy ||
1580 valType == Type::DoubleTy);
1581
1582 int64_t zeroOffset = 0; // to avoid overloading ambiguity with (Value*) 0
1583
1584 // Make a Load instruction, and make `val' both the ptr value *and*
1585 // the result value, and set the offset field to 0. Final code
1586 // generation will have to generate the base+offset for the constant.
1587 //
1588 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001589 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001590 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1591 zeroOffset);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001592 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, tmpReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001593 }
1594
Vikram S. Adve4f231662001-07-28 04:15:15 +00001595 tmpReg->addMachineInstruction(minstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001596
1597 return minstr;
1598}
1599
Vikram S. Adve4f231662001-07-28 04:15:15 +00001600//
1601// Substitute operand `operandNum' of the instruction in node `treeNode'
1602// in place the use(s) of that instruction in node `parent'.
1603//
1604static void
1605ForwardOperand(InstructionNode* treeNode,
1606 InstructionNode* parent,
1607 int operandNum)
1608{
1609 Instruction* unusedOp = treeNode->getInstruction();
1610 Value* fwdOp = unusedOp->getOperand(operandNum);
1611 Instruction* userInstr = parent->getInstruction();
1612 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1613 for (unsigned i=0, N=mvec.size(); i < N; i++)
1614 {
1615 MachineInstr* minstr = mvec[i];
1616 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1617 {
1618 const MachineOperand& mop = minstr->getOperand(i);
1619 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1620 mop.getVRegValue() == unusedOp)
1621 {
1622 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1623 fwdOp);
1624 }
1625 }
1626 }
1627}
1628
Vikram S. Advea21cf202001-07-21 12:42:19 +00001629
1630// This function is currently unused and incomplete but will be
1631// used if we have a linear layout of basic blocks in LLVM code.
1632// It decides which branch should fall-through, and whether an
1633// extra unconditional branch is needed (when neither falls through).
1634//
1635void
1636ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1637{
1638 BranchInst* brInstr = (BranchInst*) vmInstr;
1639
1640 brPattern.flipCondition = false;
1641 brPattern.targetBB = brInstr->getSuccessor(0);
1642 brPattern.extraBranch = NULL;
1643
1644 assert(brInstr->getNumSuccessors() > 1 &&
1645 "Unnecessary analysis for unconditional branch");
1646
1647 assert(0 && "Fold branches in peephole optimization");
1648}
1649