blob: 1b27d32583c645886b1c46475ae6cc1d7ce74178 [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 Lattner09ff1122002-07-24 21:21:32 +0000151 typedef 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. Adve1e606692002-07-31 21:01:34 +0000289ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290{
291 MachineOpCode opCode = INVALID_OPCODE;;
292
Vikram S. Adve1e606692002-07-31 21:01:34 +0000293 if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
294 tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000295 {
296 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000297 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000298 case Type::FloatTyID: opCode = FSTOI; break;
299 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000300 default:
301 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
302 break;
303 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000304 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000305 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000306 {
307 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000308 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000309 case Type::FloatTyID: opCode = FSTOX; break;
310 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000311 default:
312 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
313 break;
314 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000315 }
316 else
317 assert(0 && "Should not get here, Mo!");
318
319 return opCode;
320}
321
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000322MachineInstr*
Vikram S. Adve1e606692002-07-31 21:01:34 +0000323CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000324{
Vikram S. Adve1e606692002-07-31 21:01:34 +0000325 MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000326 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
327
328 MachineInstr* M = new MachineInstr(opCode);
329 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
330 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
331 return M;
332}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000333
Vikram S. Adve1e606692002-07-31 21:01:34 +0000334// CreateCodeToConvertIntToFloat: Convert FP value to signed or unsigned integer
335// The FP value must be converted to the dest type in an FP register,
336// and the result is then copied from FP to int register via memory.
337static void
338CreateCodeToConvertIntToFloat (const TargetMachine& target,
339 Value* opVal,
340 Instruction* destI,
341 std::vector<MachineInstr*>& mvec,
342 MachineCodeForInstruction& mcfi)
343{
344 // Create a temporary to represent the FP register into which the
345 // int value will placed after conversion. The type of this temporary
346 // depends on the type of FP register to use: single-prec for a 32-bit
347 // int or smaller; double-prec for a 64-bit int.
348 //
349 const Type* destTypeToUse = (destI->getType() == Type::LongTy)? Type::DoubleTy
350 : Type::FloatTy;
351 Value* destForCast = new TmpInstruction(destTypeToUse, opVal);
352 mcfi.addTemp(destForCast);
353
354 // Create the fp-to-int conversion code
355 MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
356 opVal, destForCast);
357 mvec.push_back(M);
358
359 // Create the fpreg-to-intreg copy code
360 target.getInstrInfo().
361 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
362 (TmpInstruction*)destForCast, destI, mvec, mcfi);
363}
364
365
Chris Lattner20b1ea02001-09-14 03:47:57 +0000366static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000367ChooseAddInstruction(const InstructionNode* instrNode)
368{
369 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
370}
371
372
Chris Lattner20b1ea02001-09-14 03:47:57 +0000373static inline MachineInstr*
374CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000375 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000376{
377 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000378 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000379 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
380 instrNode->leftChild()->getValue());
381 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
382 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000383 return minstr;
384}
385
386static inline MachineInstr*
387CreateAddConstInstruction(const InstructionNode* instrNode)
388{
389 MachineInstr* minstr = NULL;
390
391 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000392 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000393
394 // Cases worth optimizing are:
395 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
396 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
397 //
Chris Lattner9b625032002-05-06 16:15:30 +0000398 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
399 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000400 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000401 minstr = CreateMovFloatInstruction(instrNode,
402 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000403 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404
405 return minstr;
406}
407
408
409static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000410ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000411{
412 MachineOpCode opCode = INVALID_OPCODE;
413
Chris Lattner9b625032002-05-06 16:15:30 +0000414 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000415 {
416 opCode = SUB;
417 }
418 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000419 switch(resultType->getPrimitiveID())
420 {
421 case Type::FloatTyID: opCode = FSUBS; break;
422 case Type::DoubleTyID: opCode = FSUBD; break;
423 default: assert(0 && "Invalid type for SUB instruction"); break;
424 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000425
426 return opCode;
427}
428
429
430static inline MachineInstr*
431CreateSubConstInstruction(const InstructionNode* instrNode)
432{
433 MachineInstr* minstr = NULL;
434
435 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000436 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000437
438 // Cases worth optimizing are:
439 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
440 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
441 //
Chris Lattner9b625032002-05-06 16:15:30 +0000442 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
443 double dval = FPC->getValue();
444 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000445 minstr = CreateMovFloatInstruction(instrNode,
446 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000447 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000448
449 return minstr;
450}
451
452
453static inline MachineOpCode
454ChooseFcmpInstruction(const InstructionNode* instrNode)
455{
456 MachineOpCode opCode = INVALID_OPCODE;
457
458 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
459 switch(operand->getType()->getPrimitiveID()) {
460 case Type::FloatTyID: opCode = FCMPS; break;
461 case Type::DoubleTyID: opCode = FCMPD; break;
462 default: assert(0 && "Invalid type for FCMP instruction"); break;
463 }
464
465 return opCode;
466}
467
468
469// Assumes that leftArg and rightArg are both cast instructions.
470//
471static inline bool
472BothFloatToDouble(const InstructionNode* instrNode)
473{
474 InstrTreeNode* leftArg = instrNode->leftChild();
475 InstrTreeNode* rightArg = instrNode->rightChild();
476 InstrTreeNode* leftArgArg = leftArg->leftChild();
477 InstrTreeNode* rightArgArg = rightArg->leftChild();
478 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
479
480 // Check if both arguments are floats cast to double
481 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000482 leftArgArg->getValue()->getType() == Type::FloatTy &&
483 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000484}
485
486
487static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000488ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000489{
490 MachineOpCode opCode = INVALID_OPCODE;
491
Chris Lattner20b1ea02001-09-14 03:47:57 +0000492 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000493 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000494 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000495 switch(resultType->getPrimitiveID())
496 {
497 case Type::FloatTyID: opCode = FMULS; break;
498 case Type::DoubleTyID: opCode = FMULD; break;
499 default: assert(0 && "Invalid type for MUL instruction"); break;
500 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000501
502 return opCode;
503}
504
505
Vikram S. Adve510eec72001-11-04 21:59:14 +0000506
Chris Lattner20b1ea02001-09-14 03:47:57 +0000507static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000508CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000509 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000510{
511 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000512 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
513 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
514 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000515 return minstr;
516}
517
518
Vikram S. Adve242a8082002-05-19 15:25:51 +0000519// Create instruction sequence for any shift operation.
520// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
521// requires a second instruction for explicit sign-extension.
522// Note that we only have to worry about a sign-bit appearing in the
523// most significant bit of the operand after shifting (e.g., bit 32 of
524// Int or bit 16 of Short), so we do not have to worry about results
525// that are as large as a normal integer register.
526//
527static inline void
528CreateShiftInstructions(const TargetMachine& target,
529 Function* F,
530 MachineOpCode shiftOpCode,
531 Value* argVal1,
532 Value* optArgVal2, /* Use optArgVal2 if not NULL */
533 unsigned int optShiftNum, /* else use optShiftNum */
534 Instruction* destVal,
535 vector<MachineInstr*>& mvec,
536 MachineCodeForInstruction& mcfi)
537{
538 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
539 "Large shift sizes unexpected, but can be handled below: "
540 "You need to check whether or not it fits in immed field below");
541
542 // If this is a logical left shift of a type smaller than the standard
543 // integer reg. size, we have to extend the sign-bit into upper bits
544 // of dest, so we need to put the result of the SLL into a temporary.
545 //
546 Value* shiftDest = destVal;
547 const Type* opType = argVal1->getType();
548 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
549 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
550 && opSize < target.DataLayout.getIntegerRegize())
551 { // put SLL result into a temporary
552 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
553 mcfi.addTemp(shiftDest);
554 }
555
556 MachineInstr* M = (optArgVal2 != NULL)
557 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
558 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
559 mvec.push_back(M);
560
561 if (shiftDest != destVal)
562 { // extend the sign-bit of the result into all upper bits of dest
563 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
564 target.getInstrInfo().
565 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
566 destVal, mvec, mcfi);
567 }
568}
569
570
Vikram S. Adve74825322002-03-18 03:15:35 +0000571// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000572// create a cheaper instruction.
573// This returns the approximate cost of the instructions generated,
574// which is used to pick the cheapest when both operands are constant.
575static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000576CreateMulConstInstruction(const TargetMachine &target, Function* F,
577 Value* lval, Value* rval, Instruction* destVal,
578 vector<MachineInstr*>& mvec,
579 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000580{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000581 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000582 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000583 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000584
585 Value* constOp = rval;
586 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000587 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000588
589 // Cases worth optimizing are:
590 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
591 // (2) Multiply by 2^x for integer types: replace with Shift
592 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000593 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000594
Chris Lattner9b625032002-05-06 16:15:30 +0000595 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000596 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000597 bool isValidConst;
598 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
599 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000600 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000601 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000602 bool needNeg = false;
603 if (C < 0)
604 {
605 needNeg = true;
606 C = -C;
607 }
608
609 if (C == 0 || C == 1)
610 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000611 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000612 MachineInstr* M = (C == 0)
613 ? Create3OperandInstr_Reg(ADD,
614 target.getRegInfo().getZeroRegNum(),
615 target.getRegInfo().getZeroRegNum(),
616 destVal)
617 : Create3OperandInstr_Reg(ADD, lval,
618 target.getRegInfo().getZeroRegNum(),
619 destVal);
620 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000621 }
Chris Lattner36346c72002-05-19 21:20:19 +0000622 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000623 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000624 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
625 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
626 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
627 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000628 }
629
Vikram S. Adve242a8082002-05-19 15:25:51 +0000630 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000631 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000632 MachineInstr* M = CreateIntNegInstruction(target, destVal);
633 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000634 }
635 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000636 }
637 else
638 {
Chris Lattner9b625032002-05-06 16:15:30 +0000639 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000640 {
Chris Lattner9b625032002-05-06 16:15:30 +0000641 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000642 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000643 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000644 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000645 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
646 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000647 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
648 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000649 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000650 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000651 }
652
Vikram S. Adve242a8082002-05-19 15:25:51 +0000653 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000654 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000655 cost = 0;
656 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
657 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000658 }
659
660 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000661}
662
663
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000664// Does not create any instructions if we cannot exploit constant to
665// create a cheaper instruction.
666//
667static inline void
668CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000669 Function* F,
670 Value* lval, Value* rval,
671 Instruction* destVal,
672 vector<MachineInstr*>& mvec,
673 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000674{
675 Value* constOp;
676 if (isa<Constant>(lval) && isa<Constant>(rval))
677 { // both operands are constant: try both orders!
678 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000679 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
680 destVal, mvec1, mcfi);
681 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
682 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000683 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
684 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
685 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
686
687 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
688 delete maxcostMvec[i];
689 }
690 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000692 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000693 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000694
695 // else neither is constant
696 return;
697}
698
Vikram S. Adve74825322002-03-18 03:15:35 +0000699// Return NULL if we cannot exploit constant to create a cheaper instruction
700static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000701CreateMulInstruction(const TargetMachine &target, Function* F,
702 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000703 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000704 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000705 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
706{
707 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000708 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000709 if (mvec.size() == L)
710 { // no instructions were added so create MUL reg, reg, reg.
711 // Use FSMULD if both operands are actually floats cast to doubles.
712 // Otherwise, use the default opcode for the appropriate type.
713 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
714 ? forceMulOp
715 : ChooseMulInstructionByType(destVal->getType()));
716 MachineInstr* M = new MachineInstr(mulOp);
717 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
718 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
719 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
720 mvec.push_back(M);
721 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000722}
723
724
Vikram S. Adve510eec72001-11-04 21:59:14 +0000725// Generate a divide instruction for Div or Rem.
726// For Rem, this assumes that the operand type will be signed if the result
727// type is signed. This is correct because they must have the same sign.
728//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000729static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000730ChooseDivInstruction(TargetMachine &target,
731 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000732{
733 MachineOpCode opCode = INVALID_OPCODE;
734
735 const Type* resultType = instrNode->getInstruction()->getType();
736
737 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000738 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000739 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000740 switch(resultType->getPrimitiveID())
741 {
742 case Type::FloatTyID: opCode = FDIVS; break;
743 case Type::DoubleTyID: opCode = FDIVD; break;
744 default: assert(0 && "Invalid type for DIV instruction"); break;
745 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000746
747 return opCode;
748}
749
750
Vikram S. Adve74825322002-03-18 03:15:35 +0000751// Return NULL if we cannot exploit constant to create a cheaper instruction
752static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000753CreateDivConstInstruction(TargetMachine &target,
754 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000755 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000756{
Vikram S. Adve74825322002-03-18 03:15:35 +0000757 MachineInstr* minstr1 = NULL;
758 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000759
760 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000761 if (! isa<Constant>(constOp))
762 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000763
764 // Cases worth optimizing are:
765 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
766 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
767 //
768 const Type* resultType = instrNode->getInstruction()->getType();
769
770 if (resultType->isIntegral())
771 {
772 unsigned pow;
773 bool isValidConst;
774 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
775 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000776 {
777 bool needNeg = false;
778 if (C < 0)
779 {
780 needNeg = true;
781 C = -C;
782 }
783
784 if (C == 1)
785 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000786 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000787 minstr1->SetMachineOperandVal(0,
788 MachineOperand::MO_VirtualRegister,
789 instrNode->leftChild()->getValue());
790 minstr1->SetMachineOperandReg(1,
791 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000792 }
Chris Lattner36346c72002-05-19 21:20:19 +0000793 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000794 {
795 MachineOpCode opCode= ((resultType->isSigned())
796 ? (resultType==Type::LongTy)? SRAX : SRA
797 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000798 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000799 minstr1->SetMachineOperandVal(0,
800 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000801 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000802 minstr1->SetMachineOperandConst(1,
803 MachineOperand::MO_UnextendedImmed,
804 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000805 }
806
Vikram S. Adve74825322002-03-18 03:15:35 +0000807 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000808 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000809 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000810 instrNode->getValue());
811 }
812 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000813 }
814 else
815 {
Chris Lattner9b625032002-05-06 16:15:30 +0000816 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000817 {
Chris Lattner9b625032002-05-06 16:15:30 +0000818 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000819 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000820 {
821 bool needNeg = (dval < 0);
822
823 MachineOpCode opCode = needNeg
824 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
825 : (resultType == Type::FloatTy? FMOVS : FMOVD);
826
Vikram S. Adve74825322002-03-18 03:15:35 +0000827 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000828 minstr1->SetMachineOperandVal(0,
829 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000830 instrNode->leftChild()->getValue());
831 }
832 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000833 }
834
Vikram S. Adve74825322002-03-18 03:15:35 +0000835 if (minstr1 != NULL)
836 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
837 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000838
Vikram S. Adve74825322002-03-18 03:15:35 +0000839 if (minstr1)
840 mvec.push_back(minstr1);
841 if (minstr2)
842 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000843}
844
845
Vikram S. Adve74825322002-03-18 03:15:35 +0000846static void
847CreateCodeForVariableSizeAlloca(const TargetMachine& target,
848 Instruction* result,
849 unsigned int tsize,
850 Value* numElementsVal,
851 vector<MachineInstr*>& getMvec)
852{
853 MachineInstr* M;
854
855 // Create a Value to hold the (constant) element size
856 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
857
858 // Get the constant offset from SP for dynamically allocated storage
859 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000860 assert(result && result->getParent() && "Result value is not part of a fn?");
861 Function *F = result->getParent()->getParent();
862 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000863 bool growUp;
864 ConstantSInt* dynamicAreaOffset =
865 ConstantSInt::get(Type::IntTy,
866 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
867 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
868
869 // Create a temporary value to hold the result of MUL
870 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
871 MachineCodeForInstruction::get(result).addTemp(tmpProd);
872
873 // Instruction 1: mul numElements, typeSize -> tmpProd
874 M = new MachineInstr(MULX);
875 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
876 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
877 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
878 getMvec.push_back(M);
879
880 // Instruction 2: sub %sp, tmpProd -> %sp
881 M = new MachineInstr(SUB);
882 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
883 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
884 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
885 getMvec.push_back(M);
886
887 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
888 M = new MachineInstr(ADD);
889 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
890 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
891 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
892 getMvec.push_back(M);
893}
894
895
896static void
897CreateCodeForFixedSizeAlloca(const TargetMachine& target,
898 Instruction* result,
899 unsigned int tsize,
900 unsigned int numElements,
901 vector<MachineInstr*>& getMvec)
902{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000903 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000904 "Result value is not part of a function?");
905 Function *F = result->getParent()->getParent();
906 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000907
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000908 // Check if the offset would small enough to use as an immediate in
909 // load/stores (check LDX because all load/stores have the same-size immediate
910 // field). If not, put the variable in the dynamically sized area of the
911 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000912 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000913 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000914 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000915 tsize * numElements);
916 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
917 {
918 CreateCodeForVariableSizeAlloca(target, result, tsize,
919 ConstantSInt::get(Type::IntTy,numElements),
920 getMvec);
921 return;
922 }
923
924 // else offset fits in immediate field so go ahead and allocate it.
925 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
926
927 // Create a temporary Value to hold the constant offset.
928 // This is needed because it may not fit in the immediate field.
929 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
930
931 // Instruction 1: add %fp, offsetFromFP -> result
932 MachineInstr* M = new MachineInstr(ADD);
933 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
934 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
935 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
936
937 getMvec.push_back(M);
938}
939
940
941
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000942// Check for a constant (uint) 0.
943inline bool
944IsZero(Value* idx)
945{
946 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
947}
Vikram S. Adve242a8082002-05-19 15:25:51 +0000948
949
Chris Lattner20b1ea02001-09-14 03:47:57 +0000950//------------------------------------------------------------------------
951// Function SetOperandsForMemInstr
952//
953// Choose addressing mode for the given load or store instruction.
954// Use [reg+reg] if it is an indexed reference, and the index offset is
955// not a constant or if it cannot fit in the offset field.
956// Use [reg+offset] in all other cases.
957//
958// This assumes that all array refs are "lowered" to one of these forms:
959// %x = load (subarray*) ptr, constant ; single constant offset
960// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
961// Generally, this should happen via strength reduction + LICM.
962// Also, strength reduction should take care of using the same register for
963// the loop index variable and an array index, when that is profitable.
964//------------------------------------------------------------------------
965
966static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000967SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
968 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000969 const InstructionNode* vmInstrNode,
970 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000971{
972 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
973
Vikram S. Adve242a8082002-05-19 15:25:51 +0000974 // Variables to hold the index vector and ptr value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975 // The major work here is to extract these for all 3 instruction types
Vikram S. Adve242a8082002-05-19 15:25:51 +0000976 // and to try to fold chains of constant indices into a single offset.
977 // After that, we call SetMemOperands_Internal(), which creates the
978 // appropriate operands for the machine instruction.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000979 vector<Value*> idxVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000980 bool allConstantIndices = true;
981 Value* ptrVal = memInst->getPointerOperand();
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000982
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000983 // If there is a GetElemPtr instruction to fold in to this instr,
984 // it must be in the left child for Load and GetElemPtr, and in the
985 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000986 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000987 ? vmInstrNode->rightChild()
988 : vmInstrNode->leftChild());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000989
Vikram S. Adve242a8082002-05-19 15:25:51 +0000990 // Check if all indices are constant for this instruction
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000991 for (MemAccessInst::op_iterator OI=memInst->idx_begin(),OE=memInst->idx_end();
992 allConstantIndices && OI != OE; ++OI)
993 if (! isa<Constant>(*OI))
994 allConstantIndices = false;
995
Vikram S. Adve242a8082002-05-19 15:25:51 +0000996 // If we have only constant indices, fold chains of constant indices
997 // in this and any preceding GetElemPtr instructions.
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000998 bool foldedGEPs = false;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000999 if (allConstantIndices &&
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001000 (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
1001 ptrChild->getOpLabel() == GetElemPtrIdx))
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001002 if (Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec)) {
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001003 ptrVal = newPtr;
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001004 foldedGEPs = true;
1005 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001006
Vikram S. Adve242a8082002-05-19 15:25:51 +00001007 // Append the index vector of the current instruction, if any.
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001008 // Skip the leading [0] index if preceding GEPs were folded into this.
1009 if (memInst->getNumIndices() > 0) {
1010 assert((!foldedGEPs || IsZero(*memInst->idx_begin())) && "1st index not 0");
Chris Lattner75ac4e52002-08-03 20:57:38 +00001011 idxVec.insert(idxVec.end(),
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001012 memInst->idx_begin() + foldedGEPs, memInst->idx_end());
1013 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001014
Vikram S. Adve242a8082002-05-19 15:25:51 +00001015 // Now create the appropriate operands for the machine instruction
1016 SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
1017 ptrVal, idxVec, allConstantIndices, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001018}
1019
1020
Vikram S. Adve74825322002-03-18 03:15:35 +00001021// Generate the correct operands (and additional instructions if needed)
1022// for the given pointer and given index vector.
1023//
Chris Lattner20b1ea02001-09-14 03:47:57 +00001024static void
Vikram S. Adve74825322002-03-18 03:15:35 +00001025SetMemOperands_Internal(vector<MachineInstr*>& mvec,
1026 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001027 const InstructionNode* vmInstrNode,
1028 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001029 vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001030 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001031 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001032{
1033 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1034
1035 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001036 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001037 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001038 MachineOperand::MachineOperandType offsetOpType =
1039 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001040
Vikram S. Adve74825322002-03-18 03:15:35 +00001041 // Check if there is an index vector and if so, compute the
1042 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001043 //
1044 if (idxVec.size() > 0)
1045 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001046 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001047
Vikram S. Adve242a8082002-05-19 15:25:51 +00001048 // If all indices are constant, compute the combined offset directly.
1049 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001050 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001051 // Compute the offset value using the index vector. Create a
1052 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001053 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
1054 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001055 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001056 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001057 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001058 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001059 // be an array ref, and must have been lowered to a single non-zero
1060 // offset. (An extra leading zero offset, if any, can be ignored.)
1061 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001062 //
Chris Lattner75ac4e52002-08-03 20:57:38 +00001063 assert(idxVec.size() == 1U + IsZero(idxVec[0])
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001064 && "Array refs must be lowered before Instruction Selection");
1065
Chris Lattner75ac4e52002-08-03 20:57:38 +00001066 Value* idxVal = idxVec[IsZero(idxVec[0])];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001067
1068 vector<MachineInstr*> mulVec;
1069 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1070 MachineCodeForInstruction::get(memInst).addTemp(addr);
1071
1072 // The call to getTypeSize() will fail if size is not constant.
1073 unsigned int eltSize =
1074 target.DataLayout.getTypeSize(ptrType->getElementType());
1075 assert(eltSize > 0 && "Invalid or non-const array element size");
1076 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1077
1078 // CreateMulInstruction() folds constants intelligently enough.
1079 CreateMulInstruction(target,
1080 memInst->getParent()->getParent(),
1081 idxVal, /* lval, not likely const */
1082 eltVal, /* rval, likely constant */
1083 addr, /* result*/
1084 mulVec,
1085 MachineCodeForInstruction::get(memInst),
1086 INVALID_MACHINE_OPCODE);
1087
1088 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1089 // to point to the same instruction it pointed to before.
1090 assert(mulVec.size() > 0 && "No multiply code created?");
1091 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1092 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1093 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1094
1095 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001096 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001097 }
1098 else
1099 {
1100 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1101 smallConstOffset = 0;
1102 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001103
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001104 // For STORE:
1105 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1106 // For LOAD or GET_ELEMENT_PTR,
1107 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1108 //
1109 unsigned offsetOpNum, ptrOpNum;
1110 if (memInst->getOpcode() == Instruction::Store)
1111 {
1112 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1113 vmInstrNode->leftChild()->getValue());
1114 ptrOpNum = 1;
1115 offsetOpNum = 2;
1116 }
1117 else
1118 {
1119 ptrOpNum = 0;
1120 offsetOpNum = 1;
1121 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1122 memInst);
1123 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001124
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001125 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1126 ptrVal);
1127
Chris Lattner20b1ea02001-09-14 03:47:57 +00001128 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1129 {
1130 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1132 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001133 }
1134 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001135 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1136 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001137}
1138
1139
Chris Lattner20b1ea02001-09-14 03:47:57 +00001140//
1141// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001142// in place of the use(s) of that instruction in node `parent'.
1143// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001144// Also make sure to skip over a parent who:
1145// (1) is a list node in the Burg tree, or
1146// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001147//
1148static void
1149ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001150 InstrTreeNode* parent,
1151 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001152{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001153 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1154
Chris Lattner20b1ea02001-09-14 03:47:57 +00001155 Instruction* unusedOp = treeNode->getInstruction();
1156 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001157
1158 // The parent itself may be a list node, so find the real parent instruction
1159 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1160 {
1161 parent = parent->parent();
1162 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1163 }
1164 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1165
1166 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001167 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001168
1169 // The parent's mvec would be empty if it was itself forwarded.
1170 // Recursively call ForwardOperand in that case...
1171 //
1172 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001173 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001174 assert(parent->parent() != NULL &&
1175 "Parent could not have been forwarded, yet has no instructions?");
1176 ForwardOperand(treeNode, parent->parent(), operandNum);
1177 }
1178 else
1179 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001180 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001181 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001182 MachineInstr* minstr = mvec[i];
1183 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001184 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001185 const MachineOperand& mop = minstr->getOperand(i);
1186 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1187 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001188 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001189 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001190 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001191
1192 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1193 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001194 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001195 minstr->implicitRefIsDefined(i),
1196 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001197 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001198 }
1199}
1200
1201
Vikram S. Adve242a8082002-05-19 15:25:51 +00001202inline bool
1203AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001204{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001205 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1206 UI != UE; ++UI)
1207 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1208 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1209 return false;
1210 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001211}
1212
Vikram S. Advefb361122001-10-22 13:36:31 +00001213//******************* Externally Visible Functions *************************/
1214
Vikram S. Advefb361122001-10-22 13:36:31 +00001215//------------------------------------------------------------------------
1216// External Function: ThisIsAChainRule
1217//
1218// Purpose:
1219// Check if a given BURG rule is a chain rule.
1220//------------------------------------------------------------------------
1221
1222extern bool
1223ThisIsAChainRule(int eruleno)
1224{
1225 switch(eruleno)
1226 {
1227 case 111: // stmt: reg
1228 case 113: // stmt: bool
1229 case 123:
1230 case 124:
1231 case 125:
1232 case 126:
1233 case 127:
1234 case 128:
1235 case 129:
1236 case 130:
1237 case 131:
1238 case 132:
1239 case 133:
1240 case 155:
1241 case 221:
1242 case 222:
1243 case 241:
1244 case 242:
1245 case 243:
1246 case 244:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001247 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001248 return true; break;
1249
1250 default:
1251 return false; break;
1252 }
1253}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001254
1255
1256//------------------------------------------------------------------------
1257// External Function: GetInstructionsByRule
1258//
1259// Purpose:
1260// Choose machine instructions for the SPARC according to the
1261// patterns chosen by the BURG-generated parser.
1262//------------------------------------------------------------------------
1263
Vikram S. Adve74825322002-03-18 03:15:35 +00001264void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001265GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001266 int ruleForNode,
1267 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001268 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001269 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001270{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001271 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001272 int nextRule;
1273 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001274 unsigned int allocaSize = 0;
1275 MachineInstr* M, *M2;
1276 unsigned int L;
1277
1278 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001279
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001280 // If the code for this instruction was folded into the parent (user),
1281 // then do nothing!
1282 if (subtreeRoot->isFoldedIntoParent())
1283 return;
1284
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285 //
1286 // Let's check for chain rules outside the switch so that we don't have
1287 // to duplicate the list of chain rule production numbers here again
1288 //
1289 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001290 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001291 // Chain rules have a single nonterminal on the RHS.
1292 // Get the rule that matches the RHS non-terminal and use that instead.
1293 //
1294 assert(nts[0] && ! nts[1]
1295 && "A chain rule should have only one RHS non-terminal!");
1296 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1297 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001298 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001299 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001300 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001301 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001302 switch(ruleForNode) {
1303 case 1: // stmt: Ret
1304 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001305 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001306 // for moving return value to appropriate register.
1307 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001308 // Mark the return value register as an implicit ref of
1309 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001310 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001311 ReturnInst *returnInstr =
1312 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001313 assert(returnInstr->getOpcode() == Instruction::Ret);
1314
Chris Lattner9c461082002-02-03 07:50:56 +00001315 Instruction* returnReg = new TmpInstruction(returnInstr);
1316 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001317
Vikram S. Adve74825322002-03-18 03:15:35 +00001318 M = new MachineInstr(JMPLRET);
1319 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001320 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001321 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001322 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001323 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001324
Vikram S. Advea995e602001-10-11 04:23:19 +00001325 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001326 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001327
Vikram S. Adve74825322002-03-18 03:15:35 +00001328 mvec.push_back(M);
1329 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001331 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001332 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001333
1334 case 3: // stmt: Store(reg,reg)
1335 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001336 mvec.push_back(new MachineInstr(
1337 ChooseStoreInstruction(
1338 subtreeRoot->leftChild()->getValue()->getType())));
1339 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001340 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001341
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001343 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001344 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001345 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001346 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001347
1348 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001349 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001350 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001351
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001352 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001353 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354 // If the constant is ZERO, we can use the branch-on-integer-register
1355 // instructions and avoid the SUBcc instruction entirely.
1356 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001357 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001358 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1359 assert(constNode &&
1360 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001361 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001362 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001363
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001364 if ((constVal->getType()->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001365 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1367 && isValidConst)
1368 {
1369 // That constant is a zero after all...
1370 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001371 // Mark the setCC node so that no code is generated for it.
1372 InstructionNode* setCCNode = (InstructionNode*)
1373 subtreeRoot->leftChild();
1374 assert(setCCNode->getOpLabel() == SetCCOp);
1375 setCCNode->markFoldedIntoParent();
1376
1377 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1378
Vikram S. Adve74825322002-03-18 03:15:35 +00001379 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1380 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001381 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001382 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1383 brInst->getSuccessor(0));
1384 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001385
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001386 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001387 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388
1389 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001390 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001391 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001392 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001394
1395 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001396 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001397
1398 break;
1399 }
1400 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001401 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402
1403 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001404 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001405 // (SetCC, Not, ...). We need to check whether the type was a FP,
1406 // signed int or unsigned int, and check the branching condition in
1407 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001408 // If it is an integer CC, we also need to find the unique
1409 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001411 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001412 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001413 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001414
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001415 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1416 brInst->getParent()->getParent(),
1417 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001418
Vikram S. Adve74825322002-03-18 03:15:35 +00001419 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1420 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1421 brInst->getSuccessor(0));
1422 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001423
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001424 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001425 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001426
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001427 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001428 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001429 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001430 brInst->getSuccessor(1));
1431 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432
1433 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001434 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001435 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001436 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437
1438 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001439 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001440 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001441 Constant* constVal =
1442 cast<Constant>(subtreeRoot->leftChild()->getValue());
1443 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001444
Vikram S. Adve74825322002-03-18 03:15:35 +00001445 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001446 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001447 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001448 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001449
1450 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001451 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001453 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001454
1455 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001456 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001457 // Just use the branch-on-integer-register instruction!
1458 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001459 M = new MachineInstr(BRNZ);
1460 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001461 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001462 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001463 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001464 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001465
1466 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001467 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001468
1469 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001470 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001471 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001472 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001473 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001474
1475 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001476 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001477 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001478 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001479
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001480 case 9: // stmt: Switch(reg)
1481 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001482 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001483
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001484 case 10: // reg: VRegList(reg, reg)
1485 assert(0 && "VRegList should never be the topmost non-chain rule");
1486 break;
1487
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001488 case 21: // bool: Not(bool): Both these are implemented as:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001489 case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001490 M = new MachineInstr(XNOR);
1491 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1492 subtreeRoot->leftChild()->getValue());
1493 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1494 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1495 subtreeRoot->getValue());
1496 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001497 break;
1498
1499 case 322: // reg: ToBoolTy(bool):
1500 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001501 {
1502 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner9b625032002-05-06 16:15:30 +00001503 assert(opType->isIntegral() || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001504 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001505 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001506 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001507 }
1508
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001509 case 23: // reg: ToUByteTy(reg)
1510 case 25: // reg: ToUShortTy(reg)
1511 case 27: // reg: ToUIntTy(reg)
1512 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001513 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001514 Instruction* destI = subtreeRoot->getInstruction();
1515 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001516 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve1e606692002-07-31 21:01:34 +00001517 if (opType->isIntegral()
1518 || isa<PointerType>(opType)
1519 || opType == Type::BoolTy)
1520 {
1521 unsigned opSize = target.DataLayout.getTypeSize(opType);
1522 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1523 if (opSize > destSize ||
1524 (opType->isSigned()
1525 && destSize < target.DataLayout.getIntegerRegize()))
1526 { // operand is larger than dest,
1527 // OR both are equal but smaller than the full register size
1528 // AND operand is signed, so it may have extra sign bits:
1529 // mask high bits using AND
1530 M = Create3OperandInstr(AND, opVal,
1531 ConstantUInt::get(Type::ULongTy,
1532 ((uint64_t) 1 << 8*destSize) - 1),
1533 destI);
1534 mvec.push_back(M);
1535 }
1536 else
1537 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001538 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001539 else if (opType->isFloatingPoint())
1540 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec,
1541 MachineCodeForInstruction::get(destI));
Vikram S. Adve242a8082002-05-19 15:25:51 +00001542 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001543 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1544
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001545 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001546 }
1547
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001548 case 24: // reg: ToSByteTy(reg)
1549 case 26: // reg: ToShortTy(reg)
1550 case 28: // reg: ToIntTy(reg)
1551 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001552 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001553 Instruction* destI = subtreeRoot->getInstruction();
1554 Value* opVal = subtreeRoot->leftChild()->getValue();
1555 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001556
Vikram S. Adve242a8082002-05-19 15:25:51 +00001557 const Type* opType = opVal->getType();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001558 if (opType->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001559 || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001560 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001561 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001562 // These operand types have the same format as the destination,
1563 // but may have different size: add sign bits or mask as needed.
1564 //
1565 const Type* destType = destI->getType();
1566 unsigned opSize = target.DataLayout.getTypeSize(opType);
1567 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001568 if (opSize < destSize && !opType->isSigned())
1569 { // operand is unsigned and smaller than dest: sign-extend
Vikram S. Adve242a8082002-05-19 15:25:51 +00001570 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), opVal, 8*opSize, destI, mvec, mcfi);
1571 }
1572 else if (opSize > destSize)
1573 { // operand is larger than dest: mask high bits using AND
1574 // and then sign-extend using SRA by 0!
1575 //
1576 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1577 destI, "maskHi");
1578 mcfi.addTemp(tmpI);
1579 M = Create3OperandInstr(AND, opVal,
1580 ConstantUInt::get(Type::UIntTy,
1581 ((uint64_t) 1 << 8*destSize)-1),
1582 tmpI);
1583 mvec.push_back(M);
1584
1585 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), tmpI, 8*destSize, destI, mvec, mcfi);
1586 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001587 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001588 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001589 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001590 else if (opType->isFloatingPoint())
1591 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec, mcfi);
1592 else
1593 assert(0 && "Unrecognized operand type for convert-to-signed");
1594
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001595 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001596 }
1597
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001598 case 31: // reg: ToFloatTy(reg):
1599 case 32: // reg: ToDoubleTy(reg):
1600 case 232: // reg: ToDoubleTy(Constant):
1601
1602 // If this instruction has a parent (a user) in the tree
1603 // and the user is translated as an FsMULd instruction,
1604 // then the cast is unnecessary. So check that first.
1605 // In the future, we'll want to do the same for the FdMULq instruction,
1606 // so do the check here instead of only for ToFloatTy(reg).
1607 //
1608 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001609 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001610 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001611 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001612 }
1613 else
1614 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001615 Value* leftVal = subtreeRoot->leftChild()->getValue();
1616 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001617 MachineOpCode opCode=ChooseConvertToFloatInstr(
1618 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001619 if (opCode == INVALID_OPCODE) // no conversion needed
1620 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001621 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001622 }
1623 else
1624 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001625 // If the source operand is a non-FP type it must be
1626 // first copied from int to float register via memory!
1627 Instruction *dest = subtreeRoot->getInstruction();
1628 Value* srcForCast;
1629 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001630 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001631 {
1632 // Create a temporary to represent the FP register
1633 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001634 // The type of this temporary will determine the FP
1635 // register used: single-prec for a 32-bit int or smaller,
1636 // double-prec for a 64-bit int.
1637 //
1638 const Type* srcTypeToUse =
1639 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1640 : Type::FloatTy;
1641
Chris Lattner9c461082002-02-03 07:50:56 +00001642 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001643 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001644 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001645 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001646
Vikram S. Adve242a8082002-05-19 15:25:51 +00001647 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001648 dest->getParent()->getParent(),
1649 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001650 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001651 }
1652 else
1653 srcForCast = leftVal;
1654
Vikram S. Adve74825322002-03-18 03:15:35 +00001655 M = new MachineInstr(opCode);
1656 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1657 srcForCast);
1658 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1659 dest);
1660 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 }
1662 }
1663 break;
1664
1665 case 19: // reg: ToArrayTy(reg):
1666 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001667 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001668 break;
1669
1670 case 233: // reg: Add(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001671 M = CreateAddConstInstruction(subtreeRoot);
1672 if (M != NULL)
1673 {
1674 mvec.push_back(M);
1675 break;
1676 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001678
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 case 33: // reg: Add(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001680 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1681 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001682 break;
1683
1684 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001685 M = CreateSubConstInstruction(subtreeRoot);
1686 if (M != NULL)
1687 {
1688 mvec.push_back(M);
1689 break;
1690 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001691 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001692
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001693 case 34: // reg: Sub(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001694 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1695 subtreeRoot->getInstruction()->getType())));
1696 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001697 break;
1698
1699 case 135: // reg: Mul(todouble, todouble)
1700 checkCast = true;
1701 // FALL THROUGH
1702
1703 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001704 {
1705 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1706 ? FSMULD
1707 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001708 Instruction* mulInstr = subtreeRoot->getInstruction();
1709 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001710 subtreeRoot->leftChild()->getValue(),
1711 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001712 mulInstr, mvec,
1713 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001714 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001715 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001716 case 335: // reg: Mul(todouble, todoubleConst)
1717 checkCast = true;
1718 // FALL THROUGH
1719
1720 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001721 {
1722 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1723 ? FSMULD
1724 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001725 Instruction* mulInstr = subtreeRoot->getInstruction();
1726 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001727 subtreeRoot->leftChild()->getValue(),
1728 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001729 mulInstr, mvec,
1730 MachineCodeForInstruction::get(mulInstr),
1731 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001732 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001733 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001734 case 236: // reg: Div(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001735 L = mvec.size();
1736 CreateDivConstInstruction(target, subtreeRoot, mvec);
1737 if (mvec.size() > L)
1738 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001739 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001740
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001741 case 36: // reg: Div(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001742 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1743 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001744 break;
1745
1746 case 37: // reg: Rem(reg, reg)
1747 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001748 {
1749 Instruction* remInstr = subtreeRoot->getInstruction();
1750
Chris Lattner9c461082002-02-03 07:50:56 +00001751 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001752 subtreeRoot->leftChild()->getValue(),
1753 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001754 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001755 quot,
1756 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001757 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001758
Vikram S. Adve74825322002-03-18 03:15:35 +00001759 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1760 Set3OperandsFromInstr(M, subtreeRoot, target);
1761 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1762 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001763
Vikram S. Adve74825322002-03-18 03:15:35 +00001764 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001765 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001766 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1767 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001768 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001769 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1770 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001771
Vikram S. Adve74825322002-03-18 03:15:35 +00001772 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001773 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001774 Set3OperandsFromInstr(M, subtreeRoot, target);
1775 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1776 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001777
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001778 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001779 }
1780
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001781 case 38: // bool: And(bool, bool)
1782 case 238: // bool: And(bool, boolconst)
1783 case 338: // reg : BAnd(reg, reg)
1784 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001785 mvec.push_back(new MachineInstr(AND));
1786 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001787 break;
1788
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001789 case 138: // bool: And(bool, not)
1790 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001791 mvec.push_back(new MachineInstr(ANDN));
1792 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 break;
1794
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001795 case 39: // bool: Or(bool, bool)
1796 case 239: // bool: Or(bool, boolconst)
1797 case 339: // reg : BOr(reg, reg)
1798 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001799 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001800 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001801 break;
1802
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001803 case 139: // bool: Or(bool, not)
1804 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001805 mvec.push_back(new MachineInstr(ORN));
1806 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001807 break;
1808
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001809 case 40: // bool: Xor(bool, bool)
1810 case 240: // bool: Xor(bool, boolconst)
1811 case 340: // reg : BXor(reg, reg)
1812 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001813 mvec.push_back(new MachineInstr(XOR));
1814 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001815 break;
1816
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001817 case 140: // bool: Xor(bool, not)
1818 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001819 mvec.push_back(new MachineInstr(XNOR));
1820 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001821 break;
1822
1823 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001824 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001825 // If the SetCC was folded into the user (parent), it will be
1826 // caught above. All other cases are the same as case 42,
1827 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001828 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001829 case 42: // bool: SetCC(reg, reg):
1830 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001831 // This generates a SUBCC instruction, putting the difference in
1832 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001833 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001834 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001835 // than a branch instruction, or if it is used outside the current
1836 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001837 // computed and stored in the result register. Otherwise, discard
1838 // the difference (by using %g0) and keep only the condition code.
1839 //
1840 // To compute the boolean result in a register we use a conditional
1841 // move, unless the result of the SUBCC instruction can be used as
1842 // the bool! This assumes that zero is FALSE and any non-zero
1843 // integer is TRUE.
1844 //
1845 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1846 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001847
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001848 bool keepBoolVal = parentNode == NULL ||
1849 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001850 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001851 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1852 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1853
1854 bool mustClearReg;
1855 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001856 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001857
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001858 // Mark the 4th operand as being a CC register, and as a def
1859 // A TmpInstruction is created to represent the CC "result".
1860 // Unlike other instances of TmpInstruction, this one is used
1861 // by machine code of multiple LLVM instructions, viz.,
1862 // the SetCC and the branch. Make sure to get the same one!
1863 // Note that we do this even for FP CC registers even though they
1864 // are explicit operands, because the type of the operand
1865 // needs to be a floating point condition code, not an integer
1866 // condition code. Think of this as casting the bool result to
1867 // a FP condition code register.
1868 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001869 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001870 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001871
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001872 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1873 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001874 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001875 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001876
1877 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001878 {
1879 // Integer condition: dest. should be %g0 or an integer register.
1880 // If result must be saved but condition is not SetEQ then we need
1881 // a separate instruction to compute the bool result, so discard
1882 // result of SUBcc instruction anyway.
1883 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001884 M = new MachineInstr(SUBcc);
1885 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1886 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1887 tmpForCC, /*def*/true);
1888 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001889
1890 if (computeBoolVal)
1891 { // recompute bool using the integer condition codes
1892 movOpCode =
1893 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1894 }
1895 }
1896 else
1897 {
1898 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001899 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1900 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001901 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001902 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001903 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001904 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001905 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001906 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001907
1908 if (computeBoolVal)
1909 {// recompute bool using the FP condition codes
1910 mustClearReg = true;
1911 valueToMove = 1;
1912 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1913 }
1914 }
1915
1916 if (computeBoolVal)
1917 {
1918 if (mustClearReg)
1919 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001920 M = new MachineInstr(SETHI);
1921 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1922 (int64_t)0);
1923 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1924 setCCInstr);
1925 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001926 }
1927
1928 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001929 // Mark the register as a use (as well as a def) because the old
1930 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001931 M = new MachineInstr(movOpCode);
1932 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1933 tmpForCC);
1934 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1935 valueToMove);
1936 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001937 setCCInstr, /*isDef*/ true,
1938 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001939 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001940 }
1941 break;
1942 }
1943
1944 case 43: // boolreg: VReg
1945 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001946 break;
1947
1948 case 51: // reg: Load(reg)
1949 case 52: // reg: Load(ptrreg)
1950 case 53: // reg: LoadIdx(reg,reg)
1951 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001952 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1953 subtreeRoot->getValue()->getType())));
1954 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001955 break;
1956
1957 case 55: // reg: GetElemPtr(reg)
1958 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001959 // If the GetElemPtr was folded into the user (parent), it will be
1960 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001961 mvec.push_back(new MachineInstr(ADD));
1962 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001963 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001964
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001965 case 57: // reg: Alloca: Implement as 1 instruction:
1966 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001967 AllocationInst* instr =
1968 cast<AllocationInst>(subtreeRoot->getInstruction());
1969 unsigned int tsize =
1970 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001971 assert(tsize != 0);
1972 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001973 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001974 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001975
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001976 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1977 // mul num, typeSz -> tmp
1978 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001979 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001980 AllocationInst* instr =
1981 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001982 const Type* eltType = instr->getAllocatedType();
1983
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001984 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001985 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001986 Value* numElementsVal = NULL;
1987 bool isArray = instr->isArrayAllocation();
1988
1989 if (!isArray ||
1990 isa<Constant>(numElementsVal = instr->getArraySize()))
1991 { // total size is constant: generate code for fixed-size alloca
1992 unsigned int numElements = isArray?
1993 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1994 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1995 numElements, mvec);
1996 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001997 else // total size is not constant.
1998 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001999 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002000 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002001 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002002
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002003 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002004 { // Generate a direct (CALL) or indirect (JMPL). depending
2005 // Mark the return-address register and the indirection
2006 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002007 // Also, mark the operands of the Call and return value (if
2008 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002009 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002010 // If this is a varargs function, floating point arguments
2011 // have to passed in integer registers so insert
2012 // copy-float-to-int instructions for each float operand.
2013 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002014 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002015 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002016
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002017 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002018 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002019 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002020 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002021
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002022 // Generate the machine instruction and its operands.
2023 // Use CALL for direct function calls; this optimistically assumes
2024 // the PC-relative address fits in the CALL address field (22 bits).
2025 // Use JMPL for indirect calls.
2026 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002027 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002028 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002029 M = new MachineInstr(CALL);
2030 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2031 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002032 }
2033 else
2034 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002035 M = new MachineInstr(JMPLCALL);
2036 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2037 callee);
2038 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2039 (int64_t) 0);
2040 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2041 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002042 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002043
Vikram S. Adve74825322002-03-18 03:15:35 +00002044 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002045
Vikram S. Adve242a8082002-05-19 15:25:51 +00002046 const FunctionType* funcType =
2047 cast<FunctionType>(cast<PointerType>(callee->getType())
2048 ->getElementType());
2049 bool isVarArgs = funcType->isVarArg();
2050 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002051
Vikram S. Adve242a8082002-05-19 15:25:51 +00002052 // Use an annotation to pass information about call arguments
2053 // to the register allocator.
2054 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2055 retAddrReg, isVarArgs, noPrototype);
2056 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002057
Vikram S. Adve242a8082002-05-19 15:25:51 +00002058 assert(callInstr->getOperand(0) == callee
2059 && "This is assumed in the loop below!");
2060
2061 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2062 {
2063 Value* argVal = callInstr->getOperand(i);
2064 Instruction* intArgReg = NULL;
2065
2066 // Check for FP arguments to varargs functions.
2067 // Any such argument in the first $K$ args must be passed in an
2068 // integer register, where K = #integer argument registers.
2069 if (isVarArgs && argVal->getType()->isFloatingPoint())
2070 {
2071 // If it is a function with no prototype, pass value
2072 // as an FP value as well as a varargs value
2073 if (noPrototype)
2074 argDesc->getArgInfo(i-1).setUseFPArgReg();
2075
2076 // If this arg. is in the first $K$ regs, add a copy
2077 // float-to-int instruction to pass the value as an integer.
2078 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2079 {
2080 MachineCodeForInstruction &destMCFI =
2081 MachineCodeForInstruction::get(callInstr);
2082 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2083 destMCFI.addTemp(intArgReg);
2084
2085 vector<MachineInstr*> copyMvec;
2086 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2087 callInstr->getParent()->getParent(),
2088 argVal, (TmpInstruction*) intArgReg,
2089 copyMvec, destMCFI);
2090 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2091
2092 argDesc->getArgInfo(i-1).setUseIntArgReg();
2093 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2094 }
2095 else
2096 // Cannot fit in first $K$ regs so pass the arg on the stack
2097 argDesc->getArgInfo(i-1).setUseStackSlot();
2098 }
2099
2100 if (intArgReg)
2101 mvec.back()->addImplicitRef(intArgReg);
2102
2103 mvec.back()->addImplicitRef(argVal);
2104 }
2105
2106 // Add the return value as an implicit ref. The call operands
2107 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002108 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002109 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002110
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002111 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002112 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002113 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002114
Vikram S. Adve74825322002-03-18 03:15:35 +00002115 // delay slot
2116 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002117 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002118 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002119
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002120 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002121 {
2122 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2123 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2124 Instruction* shlInstr = subtreeRoot->getInstruction();
2125
2126 const Type* opType = argVal1->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002127 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002128 || opType == Type::BoolTy
2129 || isa<PointerType>(opType)&&"Shl unsupported for other types");
2130
2131 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2132 (opType == Type::LongTy)? SLLX : SLL,
2133 argVal1, argVal2, 0, shlInstr, mvec,
2134 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002135 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002136 }
2137
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002138 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002139 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002140 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002141 || isa<PointerType>(opType)&&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002142 mvec.push_back(new MachineInstr((opType->isSigned()
2143 ? ((opType == Type::LongTy)? SRAX : SRA)
2144 : ((opType == Type::LongTy)? SRLX : SRL))));
2145 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002146 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002147 }
2148
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002149 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002150 break; // don't forward the value
2151
Vikram S. Adve3438b212001-11-12 18:54:11 +00002152#undef NEED_PHI_MACHINE_INSTRS
2153#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002154 { // This instruction has variable #operands, so resultPos is 0.
2155 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve74825322002-03-18 03:15:35 +00002156 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2157 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002158 subtreeRoot->getValue());
2159 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve74825322002-03-18 03:15:35 +00002160 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2161 phi->getOperand(i));
2162 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002163 break;
2164 }
Chris Lattner697954c2002-01-20 22:54:45 +00002165#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002166
Vikram S. Adve74825322002-03-18 03:15:35 +00002167
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002168 case 71: // reg: VReg
2169 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002170 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002171
2172 default:
2173 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002174 break;
2175 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002176 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002177
2178 if (forwardOperandNum >= 0)
2179 { // We did not generate a machine instruction but need to use operand.
2180 // If user is in the same tree, replace Value in its machine operand.
2181 // If not, insert a copy instruction which should get coalesced away
2182 // by register allocation.
2183 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002184 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002185 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002186 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002187 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002188 Instruction* instr = subtreeRoot->getInstruction();
2189 target.getInstrInfo().
2190 CreateCopyInstructionsByType(target,
2191 instr->getParent()->getParent(),
2192 instr->getOperand(forwardOperandNum),
2193 instr, minstrVec,
2194 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002195 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002196 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002197 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002198 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002199}