blob: 668ac4a566798c573b1f2f8c72b5a2f1dd939985 [file] [log] [blame]
Vikram S. Adve243dd452001-09-18 13:03:13 +00001// $Id$
Chris Lattner20b1ea02001-09-14 03:47:57 +00002//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00007// BURS instruction selection for SPARC V9 architecture.
Chris Lattner20b1ea02001-09-14 03:47:57 +00008//
9// History:
10// 7/02/01 - Vikram Adve - Created
11//**************************************************************************/
12
13#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +000014#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +000015#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000016#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000018#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000019#include "llvm/CodeGen/InstrForest.h"
20#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner9c461082002-02-03 07:50:56 +000021#include "llvm/CodeGen/MachineCodeForMethod.h"
22#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/iTerminators.h"
25#include "llvm/iMemory.h"
26#include "llvm/iOther.h"
27#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000028#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000029#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000030#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000031#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000032using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000033
34//************************* Forward Declarations ***************************/
35
36
Vikram S. Adve74825322002-03-18 03:15:35 +000037static void SetMemOperands_Internal (vector<MachineInstr*>& mvec,
38 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000039 const InstructionNode* vmInstrNode,
40 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +000041 std::vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +000042 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000043 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000044
45
46//************************ Internal Functions ******************************/
47
Chris Lattner20b1ea02001-09-14 03:47:57 +000048
Chris Lattner20b1ea02001-09-14 03:47:57 +000049static inline MachineOpCode
50ChooseBprInstruction(const InstructionNode* instrNode)
51{
52 MachineOpCode opCode;
53
54 Instruction* setCCInstr =
55 ((InstructionNode*) instrNode->leftChild())->getInstruction();
56
57 switch(setCCInstr->getOpcode())
58 {
59 case Instruction::SetEQ: opCode = BRZ; break;
60 case Instruction::SetNE: opCode = BRNZ; break;
61 case Instruction::SetLE: opCode = BRLEZ; break;
62 case Instruction::SetGE: opCode = BRGEZ; break;
63 case Instruction::SetLT: opCode = BRLZ; break;
64 case Instruction::SetGT: opCode = BRGZ; break;
65 default:
66 assert(0 && "Unrecognized VM instruction!");
67 opCode = INVALID_OPCODE;
68 break;
69 }
70
71 return opCode;
72}
73
74
75static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000076ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000077 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000078{
79 MachineOpCode opCode = INVALID_OPCODE;
80
81 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
82
83 if (isSigned)
84 {
85 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000086 {
87 case Instruction::SetEQ: opCode = BE; break;
88 case Instruction::SetNE: opCode = BNE; break;
89 case Instruction::SetLE: opCode = BLE; break;
90 case Instruction::SetGE: opCode = BGE; break;
91 case Instruction::SetLT: opCode = BL; break;
92 case Instruction::SetGT: opCode = BG; break;
93 default:
94 assert(0 && "Unrecognized VM instruction!");
95 break;
96 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000097 }
98 else
99 {
100 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000101 {
102 case Instruction::SetEQ: opCode = BE; break;
103 case Instruction::SetNE: opCode = BNE; break;
104 case Instruction::SetLE: opCode = BLEU; break;
105 case Instruction::SetGE: opCode = BCC; break;
106 case Instruction::SetLT: opCode = BCS; break;
107 case Instruction::SetGT: opCode = BGU; break;
108 default:
109 assert(0 && "Unrecognized VM instruction!");
110 break;
111 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000112 }
113
114 return opCode;
115}
116
117static inline MachineOpCode
118ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000119 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000120{
121 MachineOpCode opCode = INVALID_OPCODE;
122
123 switch(setCCInstr->getOpcode())
124 {
125 case Instruction::SetEQ: opCode = FBE; break;
126 case Instruction::SetNE: opCode = FBNE; break;
127 case Instruction::SetLE: opCode = FBLE; break;
128 case Instruction::SetGE: opCode = FBGE; break;
129 case Instruction::SetLT: opCode = FBL; break;
130 case Instruction::SetGT: opCode = FBG; break;
131 default:
132 assert(0 && "Unrecognized VM instruction!");
133 break;
134 }
135
136 return opCode;
137}
138
139
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000140// Create a unique TmpInstruction for a boolean value,
141// representing the CC register used by a branch on that value.
142// For now, hack this using a little static cache of TmpInstructions.
143// Eventually the entire BURG instruction selection should be put
144// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000145// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000146// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147//
148static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000149GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000150{
Chris Lattner697954c2002-01-20 22:54:45 +0000151 typedef std::hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000152 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000153 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000154
155 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
156
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000157 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000158 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000159 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000160 boolToTmpCache.clear();
161 }
162
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000163 // Look for tmpI and create a new one otherwise. The new value is
164 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000165 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
166 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000167 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000168
169 return tmpI;
170}
171
172
Chris Lattner20b1ea02001-09-14 03:47:57 +0000173static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000174ChooseBccInstruction(const InstructionNode* instrNode,
175 bool& isFPBranch)
176{
177 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
178 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
179 const Type* setCCType = setCCInstr->getOperand(0)->getType();
180
Vikram S. Adve242a8082002-05-19 15:25:51 +0000181 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
182
183 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000184 return ChooseBFpccInstruction(instrNode, setCCInstr);
185 else
186 return ChooseBpccInstruction(instrNode, setCCInstr);
187}
188
189
190static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000191ChooseMovFpccInstruction(const InstructionNode* instrNode)
192{
193 MachineOpCode opCode = INVALID_OPCODE;
194
195 switch(instrNode->getInstruction()->getOpcode())
196 {
197 case Instruction::SetEQ: opCode = MOVFE; break;
198 case Instruction::SetNE: opCode = MOVFNE; break;
199 case Instruction::SetLE: opCode = MOVFLE; break;
200 case Instruction::SetGE: opCode = MOVFGE; break;
201 case Instruction::SetLT: opCode = MOVFL; break;
202 case Instruction::SetGT: opCode = MOVFG; break;
203 default:
204 assert(0 && "Unrecognized VM instruction!");
205 break;
206 }
207
208 return opCode;
209}
210
211
212// Assumes that SUBcc v1, v2 -> v3 has been executed.
213// In most cases, we want to clear v3 and then follow it by instruction
214// MOVcc 1 -> v3.
215// Set mustClearReg=false if v3 need not be cleared before conditional move.
216// Set valueToMove=0 if we want to conditionally move 0 instead of 1
217// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000218// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000219//
220static MachineOpCode
221ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000222 bool& mustClearReg,
223 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000224{
225 MachineOpCode opCode = INVALID_OPCODE;
226 mustClearReg = true;
227 valueToMove = 1;
228
229 switch(instrNode->getInstruction()->getOpcode())
230 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000231 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000232 case Instruction::SetLE: opCode = MOVLE; break;
233 case Instruction::SetGE: opCode = MOVGE; break;
234 case Instruction::SetLT: opCode = MOVL; break;
235 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000236 case Instruction::SetNE: assert(0 && "No move required!"); break;
237 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238 }
239
240 return opCode;
241}
242
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000244ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000245{
246 MachineOpCode opCode = INVALID_OPCODE;
247
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000248 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000249 {
250 case ToFloatTy:
251 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000260 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000261 break;
262
263 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000264 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
265 // Both functions should treat the integer as a 32-bit value for types
266 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000267 if (opType == Type::SByteTy || opType == Type::UByteTy ||
268 opType == Type::ShortTy || opType == Type::UShortTy ||
269 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000270 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000271 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000274 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000275 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000276 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000277 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000278 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000279 break;
280
281 default:
282 break;
283 }
284
285 return opCode;
286}
287
288static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000289ChooseConvertToIntInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290{
291 MachineOpCode opCode = INVALID_OPCODE;;
292
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000293 if (vopCode == ToSByteTy || vopCode == ToShortTy || vopCode == ToIntTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294 {
295 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000296 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 case Type::FloatTyID: opCode = FSTOI; break;
298 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000299 default:
300 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
301 break;
302 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 }
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000304 else if (vopCode == ToLongTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000305 {
306 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000307 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000308 case Type::FloatTyID: opCode = FSTOX; break;
309 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000310 default:
311 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
312 break;
313 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000314 }
315 else
316 assert(0 && "Should not get here, Mo!");
317
318 return opCode;
319}
320
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000321MachineInstr*
322CreateConvertToIntInstr(OpLabel vopCode, Value* srcVal, Value* destVal)
323{
324 MachineOpCode opCode = ChooseConvertToIntInstr(vopCode, srcVal->getType());
325 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
326
327 MachineInstr* M = new MachineInstr(opCode);
328 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
329 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
330 return M;
331}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000332
333static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000334ChooseAddInstruction(const InstructionNode* instrNode)
335{
336 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
337}
338
339
Chris Lattner20b1ea02001-09-14 03:47:57 +0000340static inline MachineInstr*
341CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000342 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000343{
344 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000345 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000346 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
347 instrNode->leftChild()->getValue());
348 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
349 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000350 return minstr;
351}
352
353static inline MachineInstr*
354CreateAddConstInstruction(const InstructionNode* instrNode)
355{
356 MachineInstr* minstr = NULL;
357
358 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000359 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000360
361 // Cases worth optimizing are:
362 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
363 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
364 //
Chris Lattner9b625032002-05-06 16:15:30 +0000365 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
366 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000367 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000368 minstr = CreateMovFloatInstruction(instrNode,
369 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000370 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000371
372 return minstr;
373}
374
375
376static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000377ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000378{
379 MachineOpCode opCode = INVALID_OPCODE;
380
Chris Lattner9b625032002-05-06 16:15:30 +0000381 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000382 {
383 opCode = SUB;
384 }
385 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000386 switch(resultType->getPrimitiveID())
387 {
388 case Type::FloatTyID: opCode = FSUBS; break;
389 case Type::DoubleTyID: opCode = FSUBD; break;
390 default: assert(0 && "Invalid type for SUB instruction"); break;
391 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000392
393 return opCode;
394}
395
396
397static inline MachineInstr*
398CreateSubConstInstruction(const InstructionNode* instrNode)
399{
400 MachineInstr* minstr = NULL;
401
402 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000403 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404
405 // Cases worth optimizing are:
406 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
407 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
408 //
Chris Lattner9b625032002-05-06 16:15:30 +0000409 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
410 double dval = FPC->getValue();
411 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000412 minstr = CreateMovFloatInstruction(instrNode,
413 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000414 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000415
416 return minstr;
417}
418
419
420static inline MachineOpCode
421ChooseFcmpInstruction(const InstructionNode* instrNode)
422{
423 MachineOpCode opCode = INVALID_OPCODE;
424
425 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
426 switch(operand->getType()->getPrimitiveID()) {
427 case Type::FloatTyID: opCode = FCMPS; break;
428 case Type::DoubleTyID: opCode = FCMPD; break;
429 default: assert(0 && "Invalid type for FCMP instruction"); break;
430 }
431
432 return opCode;
433}
434
435
436// Assumes that leftArg and rightArg are both cast instructions.
437//
438static inline bool
439BothFloatToDouble(const InstructionNode* instrNode)
440{
441 InstrTreeNode* leftArg = instrNode->leftChild();
442 InstrTreeNode* rightArg = instrNode->rightChild();
443 InstrTreeNode* leftArgArg = leftArg->leftChild();
444 InstrTreeNode* rightArgArg = rightArg->leftChild();
445 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
446
447 // Check if both arguments are floats cast to double
448 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000449 leftArgArg->getValue()->getType() == Type::FloatTy &&
450 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000451}
452
453
454static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000455ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000456{
457 MachineOpCode opCode = INVALID_OPCODE;
458
Chris Lattner20b1ea02001-09-14 03:47:57 +0000459 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000460 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000461 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000462 switch(resultType->getPrimitiveID())
463 {
464 case Type::FloatTyID: opCode = FMULS; break;
465 case Type::DoubleTyID: opCode = FMULD; break;
466 default: assert(0 && "Invalid type for MUL instruction"); break;
467 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000468
469 return opCode;
470}
471
472
Vikram S. Adve510eec72001-11-04 21:59:14 +0000473
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000475CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000476 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477{
478 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000479 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
480 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
481 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482 return minstr;
483}
484
485
Vikram S. Adve242a8082002-05-19 15:25:51 +0000486// Create instruction sequence for any shift operation.
487// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
488// requires a second instruction for explicit sign-extension.
489// Note that we only have to worry about a sign-bit appearing in the
490// most significant bit of the operand after shifting (e.g., bit 32 of
491// Int or bit 16 of Short), so we do not have to worry about results
492// that are as large as a normal integer register.
493//
494static inline void
495CreateShiftInstructions(const TargetMachine& target,
496 Function* F,
497 MachineOpCode shiftOpCode,
498 Value* argVal1,
499 Value* optArgVal2, /* Use optArgVal2 if not NULL */
500 unsigned int optShiftNum, /* else use optShiftNum */
501 Instruction* destVal,
502 vector<MachineInstr*>& mvec,
503 MachineCodeForInstruction& mcfi)
504{
505 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
506 "Large shift sizes unexpected, but can be handled below: "
507 "You need to check whether or not it fits in immed field below");
508
509 // If this is a logical left shift of a type smaller than the standard
510 // integer reg. size, we have to extend the sign-bit into upper bits
511 // of dest, so we need to put the result of the SLL into a temporary.
512 //
513 Value* shiftDest = destVal;
514 const Type* opType = argVal1->getType();
515 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
516 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
517 && opSize < target.DataLayout.getIntegerRegize())
518 { // put SLL result into a temporary
519 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
520 mcfi.addTemp(shiftDest);
521 }
522
523 MachineInstr* M = (optArgVal2 != NULL)
524 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
525 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
526 mvec.push_back(M);
527
528 if (shiftDest != destVal)
529 { // extend the sign-bit of the result into all upper bits of dest
530 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
531 target.getInstrInfo().
532 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
533 destVal, mvec, mcfi);
534 }
535}
536
537
Vikram S. Adve74825322002-03-18 03:15:35 +0000538// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000539// create a cheaper instruction.
540// This returns the approximate cost of the instructions generated,
541// which is used to pick the cheapest when both operands are constant.
542static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000543CreateMulConstInstruction(const TargetMachine &target, Function* F,
544 Value* lval, Value* rval, Instruction* destVal,
545 vector<MachineInstr*>& mvec,
546 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000547{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000548 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000549 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000550 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000551
552 Value* constOp = rval;
553 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000554 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000555
556 // Cases worth optimizing are:
557 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
558 // (2) Multiply by 2^x for integer types: replace with Shift
559 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000560 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000561
Chris Lattner9b625032002-05-06 16:15:30 +0000562 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000563 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000564 bool isValidConst;
565 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
566 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000567 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000568 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000569 bool needNeg = false;
570 if (C < 0)
571 {
572 needNeg = true;
573 C = -C;
574 }
575
576 if (C == 0 || C == 1)
577 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000578 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000579 MachineInstr* M = (C == 0)
580 ? Create3OperandInstr_Reg(ADD,
581 target.getRegInfo().getZeroRegNum(),
582 target.getRegInfo().getZeroRegNum(),
583 destVal)
584 : Create3OperandInstr_Reg(ADD, lval,
585 target.getRegInfo().getZeroRegNum(),
586 destVal);
587 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000588 }
589 else if (IsPowerOf2(C, pow))
590 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000591 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
592 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
593 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
594 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000595 }
596
Vikram S. Adve242a8082002-05-19 15:25:51 +0000597 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000598 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000599 MachineInstr* M = CreateIntNegInstruction(target, destVal);
600 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000601 }
602 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000603 }
604 else
605 {
Chris Lattner9b625032002-05-06 16:15:30 +0000606 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000607 {
Chris Lattner9b625032002-05-06 16:15:30 +0000608 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000609 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000610 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000611 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000612 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
613 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000614 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
615 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000616 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000617 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000618 }
619
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000621 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 cost = 0;
623 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
624 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000625 }
626
627 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000628}
629
630
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000631// Does not create any instructions if we cannot exploit constant to
632// create a cheaper instruction.
633//
634static inline void
635CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000636 Function* F,
637 Value* lval, Value* rval,
638 Instruction* destVal,
639 vector<MachineInstr*>& mvec,
640 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000641{
642 Value* constOp;
643 if (isa<Constant>(lval) && isa<Constant>(rval))
644 { // both operands are constant: try both orders!
645 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
647 destVal, mvec1, mcfi);
648 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
649 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000650 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
651 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
652 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
653
654 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
655 delete maxcostMvec[i];
656 }
657 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000658 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000659 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000660 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000661
662 // else neither is constant
663 return;
664}
665
Vikram S. Adve74825322002-03-18 03:15:35 +0000666// Return NULL if we cannot exploit constant to create a cheaper instruction
667static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000668CreateMulInstruction(const TargetMachine &target, Function* F,
669 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000670 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000671 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000672 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
673{
674 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000675 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000676 if (mvec.size() == L)
677 { // no instructions were added so create MUL reg, reg, reg.
678 // Use FSMULD if both operands are actually floats cast to doubles.
679 // Otherwise, use the default opcode for the appropriate type.
680 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
681 ? forceMulOp
682 : ChooseMulInstructionByType(destVal->getType()));
683 MachineInstr* M = new MachineInstr(mulOp);
684 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
685 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
686 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
687 mvec.push_back(M);
688 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000689}
690
691
Vikram S. Adve510eec72001-11-04 21:59:14 +0000692// Generate a divide instruction for Div or Rem.
693// For Rem, this assumes that the operand type will be signed if the result
694// type is signed. This is correct because they must have the same sign.
695//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000696static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000697ChooseDivInstruction(TargetMachine &target,
698 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000699{
700 MachineOpCode opCode = INVALID_OPCODE;
701
702 const Type* resultType = instrNode->getInstruction()->getType();
703
704 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000705 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000706 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000707 switch(resultType->getPrimitiveID())
708 {
709 case Type::FloatTyID: opCode = FDIVS; break;
710 case Type::DoubleTyID: opCode = FDIVD; break;
711 default: assert(0 && "Invalid type for DIV instruction"); break;
712 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000713
714 return opCode;
715}
716
717
Vikram S. Adve74825322002-03-18 03:15:35 +0000718// Return NULL if we cannot exploit constant to create a cheaper instruction
719static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000720CreateDivConstInstruction(TargetMachine &target,
721 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000722 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723{
Vikram S. Adve74825322002-03-18 03:15:35 +0000724 MachineInstr* minstr1 = NULL;
725 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726
727 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000728 if (! isa<Constant>(constOp))
729 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000730
731 // Cases worth optimizing are:
732 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
733 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
734 //
735 const Type* resultType = instrNode->getInstruction()->getType();
736
737 if (resultType->isIntegral())
738 {
739 unsigned pow;
740 bool isValidConst;
741 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
742 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000743 {
744 bool needNeg = false;
745 if (C < 0)
746 {
747 needNeg = true;
748 C = -C;
749 }
750
751 if (C == 1)
752 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000753 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000754 minstr1->SetMachineOperandVal(0,
755 MachineOperand::MO_VirtualRegister,
756 instrNode->leftChild()->getValue());
757 minstr1->SetMachineOperandReg(1,
758 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000759 }
760 else if (IsPowerOf2(C, pow))
761 {
762 MachineOpCode opCode= ((resultType->isSigned())
763 ? (resultType==Type::LongTy)? SRAX : SRA
764 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000765 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000766 minstr1->SetMachineOperandVal(0,
767 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000768 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000769 minstr1->SetMachineOperandConst(1,
770 MachineOperand::MO_UnextendedImmed,
771 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000772 }
773
Vikram S. Adve74825322002-03-18 03:15:35 +0000774 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000775 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000776 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000777 instrNode->getValue());
778 }
779 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000780 }
781 else
782 {
Chris Lattner9b625032002-05-06 16:15:30 +0000783 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000784 {
Chris Lattner9b625032002-05-06 16:15:30 +0000785 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000786 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000787 {
788 bool needNeg = (dval < 0);
789
790 MachineOpCode opCode = needNeg
791 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
792 : (resultType == Type::FloatTy? FMOVS : FMOVD);
793
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000795 minstr1->SetMachineOperandVal(0,
796 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000797 instrNode->leftChild()->getValue());
798 }
799 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000800 }
801
Vikram S. Adve74825322002-03-18 03:15:35 +0000802 if (minstr1 != NULL)
803 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
804 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000805
Vikram S. Adve74825322002-03-18 03:15:35 +0000806 if (minstr1)
807 mvec.push_back(minstr1);
808 if (minstr2)
809 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000810}
811
812
Vikram S. Adve74825322002-03-18 03:15:35 +0000813static void
814CreateCodeForVariableSizeAlloca(const TargetMachine& target,
815 Instruction* result,
816 unsigned int tsize,
817 Value* numElementsVal,
818 vector<MachineInstr*>& getMvec)
819{
820 MachineInstr* M;
821
822 // Create a Value to hold the (constant) element size
823 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
824
825 // Get the constant offset from SP for dynamically allocated storage
826 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000827 assert(result && result->getParent() && "Result value is not part of a fn?");
828 Function *F = result->getParent()->getParent();
829 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000830 bool growUp;
831 ConstantSInt* dynamicAreaOffset =
832 ConstantSInt::get(Type::IntTy,
833 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
834 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
835
836 // Create a temporary value to hold the result of MUL
837 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
838 MachineCodeForInstruction::get(result).addTemp(tmpProd);
839
840 // Instruction 1: mul numElements, typeSize -> tmpProd
841 M = new MachineInstr(MULX);
842 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
843 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
844 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
845 getMvec.push_back(M);
846
847 // Instruction 2: sub %sp, tmpProd -> %sp
848 M = new MachineInstr(SUB);
849 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
850 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
851 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
852 getMvec.push_back(M);
853
854 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
855 M = new MachineInstr(ADD);
856 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
857 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
858 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
859 getMvec.push_back(M);
860}
861
862
863static void
864CreateCodeForFixedSizeAlloca(const TargetMachine& target,
865 Instruction* result,
866 unsigned int tsize,
867 unsigned int numElements,
868 vector<MachineInstr*>& getMvec)
869{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000870 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000871 "Result value is not part of a function?");
872 Function *F = result->getParent()->getParent();
873 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000874
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000875 // Check if the offset would small enough to use as an immediate in
876 // load/stores (check LDX because all load/stores have the same-size immediate
877 // field). If not, put the variable in the dynamically sized area of the
878 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000879 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000880 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000881 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000882 tsize * numElements);
883 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
884 {
885 CreateCodeForVariableSizeAlloca(target, result, tsize,
886 ConstantSInt::get(Type::IntTy,numElements),
887 getMvec);
888 return;
889 }
890
891 // else offset fits in immediate field so go ahead and allocate it.
892 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
893
894 // Create a temporary Value to hold the constant offset.
895 // This is needed because it may not fit in the immediate field.
896 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
897
898 // Instruction 1: add %fp, offsetFromFP -> result
899 MachineInstr* M = new MachineInstr(ADD);
900 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
901 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
902 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
903
904 getMvec.push_back(M);
905}
906
907
908
Vikram S. Adve242a8082002-05-19 15:25:51 +0000909
910
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911//------------------------------------------------------------------------
912// Function SetOperandsForMemInstr
913//
914// Choose addressing mode for the given load or store instruction.
915// Use [reg+reg] if it is an indexed reference, and the index offset is
916// not a constant or if it cannot fit in the offset field.
917// Use [reg+offset] in all other cases.
918//
919// This assumes that all array refs are "lowered" to one of these forms:
920// %x = load (subarray*) ptr, constant ; single constant offset
921// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
922// Generally, this should happen via strength reduction + LICM.
923// Also, strength reduction should take care of using the same register for
924// the loop index variable and an array index, when that is profitable.
925//------------------------------------------------------------------------
926
927static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000928SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
929 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000930 const InstructionNode* vmInstrNode,
931 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000932{
933 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
934
Vikram S. Adve242a8082002-05-19 15:25:51 +0000935 // Variables to hold the index vector and ptr value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000936 // The major work here is to extract these for all 3 instruction types
Vikram S. Adve242a8082002-05-19 15:25:51 +0000937 // and to try to fold chains of constant indices into a single offset.
938 // After that, we call SetMemOperands_Internal(), which creates the
939 // appropriate operands for the machine instruction.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000940 vector<Value*> idxVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000941 bool allConstantIndices = true;
942 Value* ptrVal = memInst->getPointerOperand();
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000943
944 // If there is a GetElemPtr instruction to fold in to this instr,
945 // it must be in the left child for Load and GetElemPtr, and in the
946 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000947 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000948 ? vmInstrNode->rightChild()
949 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000950
Vikram S. Adve242a8082002-05-19 15:25:51 +0000951 // Check if all indices are constant for this instruction
952 for (MemAccessInst::op_iterator OI=memInst->idx_begin();
953 OI != memInst->idx_end(); ++OI)
954 if (! isa<ConstantUInt>(*OI))
955 {
956 allConstantIndices = false;
957 break;
958 }
959
960 // If we have only constant indices, fold chains of constant indices
961 // in this and any preceding GetElemPtr instructions.
962 if (allConstantIndices &&
963 ptrChild->getOpLabel() == Instruction::GetElementPtr ||
964 ptrChild->getOpLabel() == GetElemPtrIdx)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000965 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000966 Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
967 if (newPtr)
968 ptrVal = newPtr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969 }
970
Vikram S. Adve242a8082002-05-19 15:25:51 +0000971 // Append the index vector of the current instruction, if any.
972 // Discard any leading [0] index.
973 if (memInst->idx_begin() != memInst->idx_end())
974 {
975 ConstantUInt* CV = dyn_cast<ConstantUInt>(* memInst->idx_begin());
976 unsigned zeroOrIOne = (CV && CV->getType() == Type::UIntTy &&
977 (CV->getValue() == 0))? 1 : 0;
978 idxVec.insert(idxVec.end(),
979 memInst->idx_begin()+zeroOrIOne, memInst->idx_end());
980 }
981
982 // Now create the appropriate operands for the machine instruction
983 SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
984 ptrVal, idxVec, allConstantIndices, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000985}
986
987
Vikram S. Adve74825322002-03-18 03:15:35 +0000988// Generate the correct operands (and additional instructions if needed)
989// for the given pointer and given index vector.
990//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000991static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000992SetMemOperands_Internal(vector<MachineInstr*>& mvec,
993 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000994 const InstructionNode* vmInstrNode,
995 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000996 vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000997 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000998 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000999{
1000 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1001
1002 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001003 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001004 Value* valueForRegOffset = NULL;
1005 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
1006
Vikram S. Adve74825322002-03-18 03:15:35 +00001007 // Check if there is an index vector and if so, compute the
1008 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001009 //
1010 if (idxVec.size() > 0)
1011 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001012 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001013
Vikram S. Adve242a8082002-05-19 15:25:51 +00001014 // If all indices are constant, compute the combined offset directly.
1015 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001016 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001017 // Compute the offset value using the index vector. Create a
1018 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001019 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
1020 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001021 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001022 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001023 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001024 // There is at least one non-constant offset. Therefore, this must
1025 // be an array ref, and must have been lowered to a single offset.
Vikram S. Adve74825322002-03-18 03:15:35 +00001026 assert((memInst->getNumOperands()
1027 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
1028 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001029
Vikram S. Adve74825322002-03-18 03:15:35 +00001030 Value* arrayOffsetVal = * memInst->idx_begin();
1031
Vikram S. Adve242a8082002-05-19 15:25:51 +00001032 // Handle special common case of leading [0] index.
1033 ConstantUInt* CV = dyn_cast<ConstantUInt>(idxVec.front());
1034 bool firstIndexIsZero = bool(CV && CV->getType() == Type::UIntTy &&
1035 (CV->getValue() == 0));
1036
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001037 // If index is 0, the offset value is just 0. Otherwise,
1038 // generate a MUL instruction to compute address from index.
1039 // The call to getTypeSize() will fail if size is not constant.
1040 // CreateMulInstruction() folds constants intelligently enough.
1041 //
1042 if (firstIndexIsZero)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001043 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001044 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1045 smallConstOffset = 0;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001046 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001047 else
1048 {
1049 vector<MachineInstr*> mulVec;
1050 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1051 MachineCodeForInstruction::get(memInst).addTemp(addr);
1052
1053 unsigned int eltSize =
1054 target.DataLayout.getTypeSize(ptrType->getElementType());
1055 assert(eltSize > 0 && "Invalid or non-const array element size");
1056 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1057
1058 CreateMulInstruction(target,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001059 memInst->getParent()->getParent(),
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001060 arrayOffsetVal, /* lval, not likely const */
1061 eltVal, /* rval, likely constant */
1062 addr, /* result*/
Vikram S. Adve242a8082002-05-19 15:25:51 +00001063 mulVec,
1064 MachineCodeForInstruction::get(memInst),
1065 INVALID_MACHINE_OPCODE);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001066 assert(mulVec.size() > 0 && "No multiply instruction created?");
1067 for (vector<MachineInstr*>::const_iterator I = mulVec.begin();
1068 I != mulVec.end(); ++I)
1069 {
1070 mvecI = mvec.insert(mvecI, *I); // ptr to inserted value
1071 ++mvecI; // ptr to mem. instr.
1072 }
1073
1074 valueForRegOffset = addr;
1075 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001076 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001077 }
1078 else
1079 {
1080 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1081 smallConstOffset = 0;
1082 }
1083
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001084 // For STORE:
1085 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1086 // For LOAD or GET_ELEMENT_PTR,
1087 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1088 //
1089 unsigned offsetOpNum, ptrOpNum;
1090 if (memInst->getOpcode() == Instruction::Store)
1091 {
1092 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1093 vmInstrNode->leftChild()->getValue());
1094 ptrOpNum = 1;
1095 offsetOpNum = 2;
1096 }
1097 else
1098 {
1099 ptrOpNum = 0;
1100 offsetOpNum = 1;
1101 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1102 memInst);
1103 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001104
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001105 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1106 ptrVal);
1107
Chris Lattner20b1ea02001-09-14 03:47:57 +00001108 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1109 {
1110 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001111 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1112 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001113 }
1114 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001115 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1116 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001117}
1118
1119
Chris Lattner20b1ea02001-09-14 03:47:57 +00001120//
1121// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001122// in place of the use(s) of that instruction in node `parent'.
1123// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001124// Also make sure to skip over a parent who:
1125// (1) is a list node in the Burg tree, or
1126// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001127//
1128static void
1129ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001130 InstrTreeNode* parent,
1131 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001132{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001133 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1134
Chris Lattner20b1ea02001-09-14 03:47:57 +00001135 Instruction* unusedOp = treeNode->getInstruction();
1136 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001137
1138 // The parent itself may be a list node, so find the real parent instruction
1139 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1140 {
1141 parent = parent->parent();
1142 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1143 }
1144 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1145
1146 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001147 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001148
1149 // The parent's mvec would be empty if it was itself forwarded.
1150 // Recursively call ForwardOperand in that case...
1151 //
1152 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001153 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001154 assert(parent->parent() != NULL &&
1155 "Parent could not have been forwarded, yet has no instructions?");
1156 ForwardOperand(treeNode, parent->parent(), operandNum);
1157 }
1158 else
1159 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001160 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001161 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001162 MachineInstr* minstr = mvec[i];
1163 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001164 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001165 const MachineOperand& mop = minstr->getOperand(i);
1166 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1167 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001168 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001169 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001170 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001171
1172 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1173 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001174 minstr->setImplicitRef(i, fwdOp,
1175 minstr->implicitRefIsDefined(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001176 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001177 }
1178}
1179
1180
Vikram S. Adve242a8082002-05-19 15:25:51 +00001181inline bool
1182AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001183{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001184 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1185 UI != UE; ++UI)
1186 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1187 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1188 return false;
1189 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001190}
1191
Vikram S. Advefb361122001-10-22 13:36:31 +00001192//******************* Externally Visible Functions *************************/
1193
Vikram S. Advefb361122001-10-22 13:36:31 +00001194//------------------------------------------------------------------------
1195// External Function: ThisIsAChainRule
1196//
1197// Purpose:
1198// Check if a given BURG rule is a chain rule.
1199//------------------------------------------------------------------------
1200
1201extern bool
1202ThisIsAChainRule(int eruleno)
1203{
1204 switch(eruleno)
1205 {
1206 case 111: // stmt: reg
1207 case 113: // stmt: bool
1208 case 123:
1209 case 124:
1210 case 125:
1211 case 126:
1212 case 127:
1213 case 128:
1214 case 129:
1215 case 130:
1216 case 131:
1217 case 132:
1218 case 133:
1219 case 155:
1220 case 221:
1221 case 222:
1222 case 241:
1223 case 242:
1224 case 243:
1225 case 244:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001226 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001227 return true; break;
1228
1229 default:
1230 return false; break;
1231 }
1232}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001233
1234
1235//------------------------------------------------------------------------
1236// External Function: GetInstructionsByRule
1237//
1238// Purpose:
1239// Choose machine instructions for the SPARC according to the
1240// patterns chosen by the BURG-generated parser.
1241//------------------------------------------------------------------------
1242
Vikram S. Adve74825322002-03-18 03:15:35 +00001243void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001244GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001245 int ruleForNode,
1246 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001247 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001248 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001249{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001250 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001251 int nextRule;
1252 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001253 unsigned int allocaSize = 0;
1254 MachineInstr* M, *M2;
1255 unsigned int L;
1256
1257 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001258
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001259 // If the code for this instruction was folded into the parent (user),
1260 // then do nothing!
1261 if (subtreeRoot->isFoldedIntoParent())
1262 return;
1263
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001264 //
1265 // Let's check for chain rules outside the switch so that we don't have
1266 // to duplicate the list of chain rule production numbers here again
1267 //
1268 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001269 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001270 // Chain rules have a single nonterminal on the RHS.
1271 // Get the rule that matches the RHS non-terminal and use that instead.
1272 //
1273 assert(nts[0] && ! nts[1]
1274 && "A chain rule should have only one RHS non-terminal!");
1275 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1276 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001277 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001278 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001279 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001280 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001281 switch(ruleForNode) {
1282 case 1: // stmt: Ret
1283 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001284 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285 // for moving return value to appropriate register.
1286 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001287 // Mark the return value register as an implicit ref of
1288 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001289 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001290 ReturnInst *returnInstr =
1291 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292 assert(returnInstr->getOpcode() == Instruction::Ret);
1293
Chris Lattner9c461082002-02-03 07:50:56 +00001294 Instruction* returnReg = new TmpInstruction(returnInstr);
1295 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001296
Vikram S. Adve74825322002-03-18 03:15:35 +00001297 M = new MachineInstr(JMPLRET);
1298 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001299 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001300 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001301 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001302 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001303
Vikram S. Advea995e602001-10-11 04:23:19 +00001304 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001305 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001306
Vikram S. Adve74825322002-03-18 03:15:35 +00001307 mvec.push_back(M);
1308 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001309
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001311 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001312
1313 case 3: // stmt: Store(reg,reg)
1314 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001315 mvec.push_back(new MachineInstr(
1316 ChooseStoreInstruction(
1317 subtreeRoot->leftChild()->getValue()->getType())));
1318 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001319 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001320
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001322 M = new MachineInstr(BA);
1323 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001324 (Value*)NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001325 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001326 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001327 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001328
1329 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001330 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001331 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001332
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001333 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001334 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001335 // If the constant is ZERO, we can use the branch-on-integer-register
1336 // instructions and avoid the SUBcc instruction entirely.
1337 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001338 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001339 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1340 assert(constNode &&
1341 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001342 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001344
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345 if ((constVal->getType()->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001346 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001347 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1348 && isValidConst)
1349 {
1350 // That constant is a zero after all...
1351 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001352 // Mark the setCC node so that no code is generated for it.
1353 InstructionNode* setCCNode = (InstructionNode*)
1354 subtreeRoot->leftChild();
1355 assert(setCCNode->getOpLabel() == SetCCOp);
1356 setCCNode->markFoldedIntoParent();
1357
1358 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1359
Vikram S. Adve74825322002-03-18 03:15:35 +00001360 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1361 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001362 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001363 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1364 brInst->getSuccessor(0));
1365 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001366
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001368 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369
1370 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001371 M = new MachineInstr(BA);
1372 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1373 (Value*) NULL);
1374 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001375 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377
1378 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001379 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380
1381 break;
1382 }
1383 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001384 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385
1386 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001387 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388 // (SetCC, Not, ...). We need to check whether the type was a FP,
1389 // signed int or unsigned int, and check the branching condition in
1390 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001391 // If it is an integer CC, we also need to find the unique
1392 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001393 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001394 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001395 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001396 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001397
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001398 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1399 brInst->getParent()->getParent(),
1400 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001401
Vikram S. Adve74825322002-03-18 03:15:35 +00001402 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1403 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1404 brInst->getSuccessor(0));
1405 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001406
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001408 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001409
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001411 M = new MachineInstr(BA);
1412 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1413 (Value*) NULL);
1414 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1415 brInst->getSuccessor(1));
1416 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001417
1418 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001419 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001420 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001421 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001422
1423 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001424 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001425 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001426 Constant* constVal =
1427 cast<Constant>(subtreeRoot->leftChild()->getValue());
1428 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001429
Vikram S. Adve74825322002-03-18 03:15:35 +00001430 M = new MachineInstr(BA);
1431 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1432 (Value*) NULL);
1433 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001434 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001435 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001436
1437 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001438 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001439 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001440 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441
1442 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001443 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001444 // Just use the branch-on-integer-register instruction!
1445 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001446 M = new MachineInstr(BRNZ);
1447 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001448 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001449 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001450 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001451 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452
1453 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001454 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001455
1456 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001457 M = new MachineInstr(BA);
1458 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1459 (Value*) NULL);
1460 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001461 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001462 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001463
1464 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001465 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001466 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001467 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001468
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001469 case 9: // stmt: Switch(reg)
1470 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001471 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001472
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001473 case 10: // reg: VRegList(reg, reg)
1474 assert(0 && "VRegList should never be the topmost non-chain rule");
1475 break;
1476
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001477 case 21: // bool: Not(bool): Both these are implemented as:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001478 case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001479 M = new MachineInstr(XNOR);
1480 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1481 subtreeRoot->leftChild()->getValue());
1482 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1483 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1484 subtreeRoot->getValue());
1485 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001486 break;
1487
1488 case 322: // reg: ToBoolTy(bool):
1489 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001490 {
1491 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner9b625032002-05-06 16:15:30 +00001492 assert(opType->isIntegral() || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001493 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001494 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001495 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001496 }
1497
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001498 case 23: // reg: ToUByteTy(reg)
1499 case 25: // reg: ToUShortTy(reg)
1500 case 27: // reg: ToUIntTy(reg)
1501 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001502 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001503 Instruction* destI = subtreeRoot->getInstruction();
1504 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001505 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001506 assert(opType->isIntegral() ||
Chris Lattner9b625032002-05-06 16:15:30 +00001507 isa<PointerType>(opType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001508 opType == Type::BoolTy && "Cast is illegal for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00001509
1510 unsigned opSize = target.DataLayout.getTypeSize(opType);
1511 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1512
1513 if (opSize > destSize ||
1514 (opType->isSigned()
1515 && destSize < target.DataLayout.getIntegerRegize()))
1516 { // operand is larger than dest,
1517 // OR both are equal but smaller than the full register size
1518 // AND operand is signed, so it may have extra sign bits:
1519 // mask high bits using AND
1520 //
1521 M = Create3OperandInstr(AND, opVal,
1522 ConstantUInt::get(Type::ULongTy,
1523 ((uint64_t) 1 << 8*destSize) - 1),
1524 destI);
1525 mvec.push_back(M);
1526 }
1527 else
1528 forwardOperandNum = 0; // forward first operand to user
1529
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001530 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001531 }
1532
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533 case 24: // reg: ToSByteTy(reg)
1534 case 26: // reg: ToShortTy(reg)
1535 case 28: // reg: ToIntTy(reg)
1536 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001537 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001538 unsigned int oldMvecSize = mvec.size(); // to check if it grew
1539 Instruction* destI = subtreeRoot->getInstruction();
1540 Value* opVal = subtreeRoot->leftChild()->getValue();
1541 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
1542
1543 const Type* opType = opVal->getType();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001544 if (opType->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001545 || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001546 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001547 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001548 // These operand types have the same format as the destination,
1549 // but may have different size: add sign bits or mask as needed.
1550 //
1551 const Type* destType = destI->getType();
1552 unsigned opSize = target.DataLayout.getTypeSize(opType);
1553 unsigned destSize = target.DataLayout.getTypeSize(destType);
1554 if (opSize <= destSize && !opType->isSigned())
1555 { // operand is smaller than or same size as dest:
1556 // -- if operand is signed (checked above), nothing to do
1557 // -- if operand is unsigned, sign-extend the value:
1558 //
1559 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), opVal, 8*opSize, destI, mvec, mcfi);
1560 }
1561 else if (opSize > destSize)
1562 { // operand is larger than dest: mask high bits using AND
1563 // and then sign-extend using SRA by 0!
1564 //
1565 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1566 destI, "maskHi");
1567 mcfi.addTemp(tmpI);
1568 M = Create3OperandInstr(AND, opVal,
1569 ConstantUInt::get(Type::UIntTy,
1570 ((uint64_t) 1 << 8*destSize)-1),
1571 tmpI);
1572 mvec.push_back(M);
1573
1574 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), tmpI, 8*destSize, destI, mvec, mcfi);
1575 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001576 }
1577 else
1578 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001579 // If the source operand is an FP type, the int result must be
1580 // copied from float to int register via memory!
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001581 Value* leftVal = subtreeRoot->leftChild()->getValue();
1582 Value* destForCast;
1583 vector<MachineInstr*> minstrVec;
1584
Chris Lattner9b625032002-05-06 16:15:30 +00001585 if (opType->isFloatingPoint())
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001586 {
1587 // Create a temporary to represent the INT register
1588 // into which the FP value will be copied via memory.
1589 // The type of this temporary will determine the FP
1590 // register used: single-prec for a 32-bit int or smaller,
1591 // double-prec for a 64-bit int.
1592 //
1593 const Type* destTypeToUse =
Vikram S. Adve242a8082002-05-19 15:25:51 +00001594 (destI->getType() == Type::LongTy)? Type::DoubleTy
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001595 : Type::FloatTy;
Chris Lattner9c461082002-02-03 07:50:56 +00001596 destForCast = new TmpInstruction(destTypeToUse, leftVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001597 MachineCodeForInstruction &destMCFI =
Vikram S. Adve242a8082002-05-19 15:25:51 +00001598 MachineCodeForInstruction::get(destI);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001599 destMCFI.addTemp(destForCast);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001600
Vikram S. Adve242a8082002-05-19 15:25:51 +00001601 target.getInstrInfo().
1602 CreateCodeToCopyFloatToInt(target,
1603 destI->getParent()->getParent(),
1604 (TmpInstruction*) destForCast,
1605 destI, minstrVec, destMCFI);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001606 }
1607 else
1608 destForCast = leftVal;
1609
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001610 M = CreateConvertToIntInstr(subtreeRoot->getOpLabel(),
1611 leftVal, destForCast);
Vikram S. Adve74825322002-03-18 03:15:35 +00001612 mvec.push_back(M);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001613
Vikram S. Adve74825322002-03-18 03:15:35 +00001614 // Append the copy code, if any, after the conversion instr.
1615 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001616 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001617
1618 if (oldMvecSize == mvec.size()) // no instruction was generated
1619 forwardOperandNum = 0; // forward first operand to user
1620
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001622 }
1623
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001624 case 31: // reg: ToFloatTy(reg):
1625 case 32: // reg: ToDoubleTy(reg):
1626 case 232: // reg: ToDoubleTy(Constant):
1627
1628 // If this instruction has a parent (a user) in the tree
1629 // and the user is translated as an FsMULd instruction,
1630 // then the cast is unnecessary. So check that first.
1631 // In the future, we'll want to do the same for the FdMULq instruction,
1632 // so do the check here instead of only for ToFloatTy(reg).
1633 //
1634 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001635 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001636 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001637 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001638 }
1639 else
1640 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001641 Value* leftVal = subtreeRoot->leftChild()->getValue();
1642 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001643 MachineOpCode opCode=ChooseConvertToFloatInstr(
1644 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001645 if (opCode == INVALID_OPCODE) // no conversion needed
1646 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001647 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001648 }
1649 else
1650 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001651 // If the source operand is a non-FP type it must be
1652 // first copied from int to float register via memory!
1653 Instruction *dest = subtreeRoot->getInstruction();
1654 Value* srcForCast;
1655 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001656 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001657 {
1658 // Create a temporary to represent the FP register
1659 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001660 // The type of this temporary will determine the FP
1661 // register used: single-prec for a 32-bit int or smaller,
1662 // double-prec for a 64-bit int.
1663 //
1664 const Type* srcTypeToUse =
1665 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1666 : Type::FloatTy;
1667
Chris Lattner9c461082002-02-03 07:50:56 +00001668 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001669 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001670 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001671 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001672
Vikram S. Adve242a8082002-05-19 15:25:51 +00001673 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001674 dest->getParent()->getParent(),
1675 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001676 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001677 }
1678 else
1679 srcForCast = leftVal;
1680
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 M = new MachineInstr(opCode);
1682 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1683 srcForCast);
1684 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1685 dest);
1686 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 }
1688 }
1689 break;
1690
1691 case 19: // reg: ToArrayTy(reg):
1692 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001693 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 break;
1695
1696 case 233: // reg: Add(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001697 M = CreateAddConstInstruction(subtreeRoot);
1698 if (M != NULL)
1699 {
1700 mvec.push_back(M);
1701 break;
1702 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001704
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001705 case 33: // reg: Add(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001706 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1707 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001708 break;
1709
1710 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001711 M = CreateSubConstInstruction(subtreeRoot);
1712 if (M != NULL)
1713 {
1714 mvec.push_back(M);
1715 break;
1716 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001717 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001718
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001719 case 34: // reg: Sub(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001720 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1721 subtreeRoot->getInstruction()->getType())));
1722 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001723 break;
1724
1725 case 135: // reg: Mul(todouble, todouble)
1726 checkCast = true;
1727 // FALL THROUGH
1728
1729 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001730 {
1731 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1732 ? FSMULD
1733 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001734 Instruction* mulInstr = subtreeRoot->getInstruction();
1735 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001736 subtreeRoot->leftChild()->getValue(),
1737 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001738 mulInstr, mvec,
1739 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001740 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001741 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001742 case 335: // reg: Mul(todouble, todoubleConst)
1743 checkCast = true;
1744 // FALL THROUGH
1745
1746 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001747 {
1748 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1749 ? FSMULD
1750 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001751 Instruction* mulInstr = subtreeRoot->getInstruction();
1752 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001753 subtreeRoot->leftChild()->getValue(),
1754 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001755 mulInstr, mvec,
1756 MachineCodeForInstruction::get(mulInstr),
1757 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001758 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001759 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760 case 236: // reg: Div(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001761 L = mvec.size();
1762 CreateDivConstInstruction(target, subtreeRoot, mvec);
1763 if (mvec.size() > L)
1764 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001766
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001767 case 36: // reg: Div(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001768 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1769 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001770 break;
1771
1772 case 37: // reg: Rem(reg, reg)
1773 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001774 {
1775 Instruction* remInstr = subtreeRoot->getInstruction();
1776
Chris Lattner9c461082002-02-03 07:50:56 +00001777 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001778 subtreeRoot->leftChild()->getValue(),
1779 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001780 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001781 quot,
1782 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001783 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001784
Vikram S. Adve74825322002-03-18 03:15:35 +00001785 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1786 Set3OperandsFromInstr(M, subtreeRoot, target);
1787 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1788 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001789
Vikram S. Adve74825322002-03-18 03:15:35 +00001790 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001791 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001792 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1793 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001794 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001795 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1796 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001797
Vikram S. Adve74825322002-03-18 03:15:35 +00001798 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001799 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001800 Set3OperandsFromInstr(M, subtreeRoot, target);
1801 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1802 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001803
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001804 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001805 }
1806
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001807 case 38: // bool: And(bool, bool)
1808 case 238: // bool: And(bool, boolconst)
1809 case 338: // reg : BAnd(reg, reg)
1810 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001811 mvec.push_back(new MachineInstr(AND));
1812 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001813 break;
1814
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001815 case 138: // bool: And(bool, not)
1816 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001817 mvec.push_back(new MachineInstr(ANDN));
1818 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001819 break;
1820
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001821 case 39: // bool: Or(bool, bool)
1822 case 239: // bool: Or(bool, boolconst)
1823 case 339: // reg : BOr(reg, reg)
1824 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001825 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001826 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001827 break;
1828
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001829 case 139: // bool: Or(bool, not)
1830 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001831 mvec.push_back(new MachineInstr(ORN));
1832 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001833 break;
1834
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001835 case 40: // bool: Xor(bool, bool)
1836 case 240: // bool: Xor(bool, boolconst)
1837 case 340: // reg : BXor(reg, reg)
1838 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001839 mvec.push_back(new MachineInstr(XOR));
1840 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001841 break;
1842
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001843 case 140: // bool: Xor(bool, not)
1844 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001845 mvec.push_back(new MachineInstr(XNOR));
1846 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001847 break;
1848
1849 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001850 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001851 // If the SetCC was folded into the user (parent), it will be
1852 // caught above. All other cases are the same as case 42,
1853 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001854 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001855 case 42: // bool: SetCC(reg, reg):
1856 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001857 // This generates a SUBCC instruction, putting the difference in
1858 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001859 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001860 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve242a8082002-05-19 15:25:51 +00001861 // than a branch instruction, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001862 // computed and stored in the result register. Otherwise, discard
1863 // the difference (by using %g0) and keep only the condition code.
1864 //
1865 // To compute the boolean result in a register we use a conditional
1866 // move, unless the result of the SUBCC instruction can be used as
1867 // the bool! This assumes that zero is FALSE and any non-zero
1868 // integer is TRUE.
1869 //
1870 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1871 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001872
1873 bool keepBoolVal = ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001874 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001875 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1876 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1877
1878 bool mustClearReg;
1879 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001880 MachineOpCode movOpCode = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001881
1882 // Mark the 4th operand as being a CC register, and as a def
1883 // A TmpInstruction is created to represent the CC "result".
1884 // Unlike other instances of TmpInstruction, this one is used
1885 // by machine code of multiple LLVM instructions, viz.,
1886 // the SetCC and the branch. Make sure to get the same one!
1887 // Note that we do this even for FP CC registers even though they
1888 // are explicit operands, because the type of the operand
1889 // needs to be a floating point condition code, not an integer
1890 // condition code. Think of this as casting the bool result to
1891 // a FP condition code register.
1892 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001893 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001894 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001895
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001896 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1897 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001898 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001899 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001900
1901 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001902 {
1903 // Integer condition: dest. should be %g0 or an integer register.
1904 // If result must be saved but condition is not SetEQ then we need
1905 // a separate instruction to compute the bool result, so discard
1906 // result of SUBcc instruction anyway.
1907 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001908 M = new MachineInstr(SUBcc);
1909 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1910 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1911 tmpForCC, /*def*/true);
1912 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001913
1914 if (computeBoolVal)
1915 { // recompute bool using the integer condition codes
1916 movOpCode =
1917 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1918 }
1919 }
1920 else
1921 {
1922 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001923 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1924 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001925 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001926 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001927 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001928 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001929 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001930 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001931
1932 if (computeBoolVal)
1933 {// recompute bool using the FP condition codes
1934 mustClearReg = true;
1935 valueToMove = 1;
1936 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1937 }
1938 }
1939
1940 if (computeBoolVal)
1941 {
1942 if (mustClearReg)
1943 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001944 M = new MachineInstr(SETHI);
1945 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1946 (int64_t)0);
1947 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1948 setCCInstr);
1949 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001950 }
1951
1952 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve74825322002-03-18 03:15:35 +00001953 M = new MachineInstr(movOpCode);
1954 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1955 tmpForCC);
1956 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1957 valueToMove);
1958 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1959 setCCInstr);
1960 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001961 }
1962 break;
1963 }
1964
1965 case 43: // boolreg: VReg
1966 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001967 break;
1968
1969 case 51: // reg: Load(reg)
1970 case 52: // reg: Load(ptrreg)
1971 case 53: // reg: LoadIdx(reg,reg)
1972 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001973 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1974 subtreeRoot->getValue()->getType())));
1975 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001976 break;
1977
1978 case 55: // reg: GetElemPtr(reg)
1979 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001980 // If the GetElemPtr was folded into the user (parent), it will be
1981 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001982 mvec.push_back(new MachineInstr(ADD));
1983 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001984 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001985
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001986 case 57: // reg: Alloca: Implement as 1 instruction:
1987 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001988 AllocationInst* instr =
1989 cast<AllocationInst>(subtreeRoot->getInstruction());
1990 unsigned int tsize =
1991 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001992 assert(tsize != 0);
1993 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001994 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001995 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001996
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001997 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1998 // mul num, typeSz -> tmp
1999 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002000 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002001 AllocationInst* instr =
2002 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002003 const Type* eltType = instr->getAllocatedType();
2004
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002005 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002006 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002007 Value* numElementsVal = NULL;
2008 bool isArray = instr->isArrayAllocation();
2009
2010 if (!isArray ||
2011 isa<Constant>(numElementsVal = instr->getArraySize()))
2012 { // total size is constant: generate code for fixed-size alloca
2013 unsigned int numElements = isArray?
2014 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2015 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2016 numElements, mvec);
2017 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002018 else // total size is not constant.
2019 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002020 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002021 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002022 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002023
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002024 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002025 { // Generate a direct (CALL) or indirect (JMPL). depending
2026 // Mark the return-address register and the indirection
2027 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002028 // Also, mark the operands of the Call and return value (if
2029 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002030 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002031 // If this is a varargs function, floating point arguments
2032 // have to passed in integer registers so insert
2033 // copy-float-to-int instructions for each float operand.
2034 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002035 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002036 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002037
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002038 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002039 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002040 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002041 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002042
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002043 // Generate the machine instruction and its operands.
2044 // Use CALL for direct function calls; this optimistically assumes
2045 // the PC-relative address fits in the CALL address field (22 bits).
2046 // Use JMPL for indirect calls.
2047 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002048 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002049 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002050 M = new MachineInstr(CALL);
2051 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2052 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002053 }
2054 else
2055 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002056 M = new MachineInstr(JMPLCALL);
2057 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2058 callee);
2059 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2060 (int64_t) 0);
2061 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2062 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002063 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002064
Vikram S. Adve74825322002-03-18 03:15:35 +00002065 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002066
Vikram S. Adve242a8082002-05-19 15:25:51 +00002067 const FunctionType* funcType =
2068 cast<FunctionType>(cast<PointerType>(callee->getType())
2069 ->getElementType());
2070 bool isVarArgs = funcType->isVarArg();
2071 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002072
Vikram S. Adve242a8082002-05-19 15:25:51 +00002073 // Use an annotation to pass information about call arguments
2074 // to the register allocator.
2075 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2076 retAddrReg, isVarArgs, noPrototype);
2077 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002078
Vikram S. Adve242a8082002-05-19 15:25:51 +00002079 assert(callInstr->getOperand(0) == callee
2080 && "This is assumed in the loop below!");
2081
2082 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2083 {
2084 Value* argVal = callInstr->getOperand(i);
2085 Instruction* intArgReg = NULL;
2086
2087 // Check for FP arguments to varargs functions.
2088 // Any such argument in the first $K$ args must be passed in an
2089 // integer register, where K = #integer argument registers.
2090 if (isVarArgs && argVal->getType()->isFloatingPoint())
2091 {
2092 // If it is a function with no prototype, pass value
2093 // as an FP value as well as a varargs value
2094 if (noPrototype)
2095 argDesc->getArgInfo(i-1).setUseFPArgReg();
2096
2097 // If this arg. is in the first $K$ regs, add a copy
2098 // float-to-int instruction to pass the value as an integer.
2099 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2100 {
2101 MachineCodeForInstruction &destMCFI =
2102 MachineCodeForInstruction::get(callInstr);
2103 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2104 destMCFI.addTemp(intArgReg);
2105
2106 vector<MachineInstr*> copyMvec;
2107 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2108 callInstr->getParent()->getParent(),
2109 argVal, (TmpInstruction*) intArgReg,
2110 copyMvec, destMCFI);
2111 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2112
2113 argDesc->getArgInfo(i-1).setUseIntArgReg();
2114 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2115 }
2116 else
2117 // Cannot fit in first $K$ regs so pass the arg on the stack
2118 argDesc->getArgInfo(i-1).setUseStackSlot();
2119 }
2120
2121 if (intArgReg)
2122 mvec.back()->addImplicitRef(intArgReg);
2123
2124 mvec.back()->addImplicitRef(argVal);
2125 }
2126
2127 // Add the return value as an implicit ref. The call operands
2128 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002129 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002130 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002131
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002132 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002133 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002134 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002135
Vikram S. Adve74825322002-03-18 03:15:35 +00002136 // delay slot
2137 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002138 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002139 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002140
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002142 {
2143 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2144 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2145 Instruction* shlInstr = subtreeRoot->getInstruction();
2146
2147 const Type* opType = argVal1->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002148 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002149 || opType == Type::BoolTy
2150 || isa<PointerType>(opType)&&"Shl unsupported for other types");
2151
2152 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2153 (opType == Type::LongTy)? SLLX : SLL,
2154 argVal1, argVal2, 0, shlInstr, mvec,
2155 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002156 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002157 }
2158
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002159 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002160 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002161 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002162 || isa<PointerType>(opType)&&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002163 mvec.push_back(new MachineInstr((opType->isSigned()
2164 ? ((opType == Type::LongTy)? SRAX : SRA)
2165 : ((opType == Type::LongTy)? SRLX : SRL))));
2166 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002167 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002168 }
2169
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002170 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002171 break; // don't forward the value
2172
Vikram S. Adve3438b212001-11-12 18:54:11 +00002173#undef NEED_PHI_MACHINE_INSTRS
2174#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002175 { // This instruction has variable #operands, so resultPos is 0.
2176 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve74825322002-03-18 03:15:35 +00002177 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2178 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002179 subtreeRoot->getValue());
2180 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve74825322002-03-18 03:15:35 +00002181 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2182 phi->getOperand(i));
2183 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002184 break;
2185 }
Chris Lattner697954c2002-01-20 22:54:45 +00002186#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002187
Vikram S. Adve74825322002-03-18 03:15:35 +00002188
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002189 case 71: // reg: VReg
2190 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002191 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002192
2193 default:
2194 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002195 break;
2196 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002197 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002198
2199 if (forwardOperandNum >= 0)
2200 { // We did not generate a machine instruction but need to use operand.
2201 // If user is in the same tree, replace Value in its machine operand.
2202 // If not, insert a copy instruction which should get coalesced away
2203 // by register allocation.
2204 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002205 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002206 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002207 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002208 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002209 Instruction* instr = subtreeRoot->getInstruction();
2210 target.getInstrInfo().
2211 CreateCopyInstructionsByType(target,
2212 instr->getParent()->getParent(),
2213 instr->getOperand(forwardOperandNum),
2214 instr, minstrVec,
2215 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002216 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002217 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002218 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002219 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002220}