blob: 84a9eb68f3abac23e94fa6bcec8059306a0d8990 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000024#include "llvm/ConstantHandling.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000026#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
Chris Lattner54e898e2003-01-15 19:23:34 +000029static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
30 vector<MachineInstr*>& mvec) {
31 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
32 .addReg(Node->rightChild()->getValue())
33 .addRegDef(Node->getValue()));
34}
35
36
37
38
Chris Lattner20b1ea02001-09-14 03:47:57 +000039//************************ Internal Functions ******************************/
40
Chris Lattner20b1ea02001-09-14 03:47:57 +000041
Chris Lattner20b1ea02001-09-14 03:47:57 +000042static inline MachineOpCode
43ChooseBprInstruction(const InstructionNode* instrNode)
44{
45 MachineOpCode opCode;
46
47 Instruction* setCCInstr =
48 ((InstructionNode*) instrNode->leftChild())->getInstruction();
49
50 switch(setCCInstr->getOpcode())
51 {
52 case Instruction::SetEQ: opCode = BRZ; break;
53 case Instruction::SetNE: opCode = BRNZ; break;
54 case Instruction::SetLE: opCode = BRLEZ; break;
55 case Instruction::SetGE: opCode = BRGEZ; break;
56 case Instruction::SetLT: opCode = BRLZ; break;
57 case Instruction::SetGT: opCode = BRGZ; break;
58 default:
59 assert(0 && "Unrecognized VM instruction!");
60 opCode = INVALID_OPCODE;
61 break;
62 }
63
64 return opCode;
65}
66
67
68static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000069ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000070 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000071{
72 MachineOpCode opCode = INVALID_OPCODE;
73
74 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
75
76 if (isSigned)
77 {
78 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000079 {
80 case Instruction::SetEQ: opCode = BE; break;
81 case Instruction::SetNE: opCode = BNE; break;
82 case Instruction::SetLE: opCode = BLE; break;
83 case Instruction::SetGE: opCode = BGE; break;
84 case Instruction::SetLT: opCode = BL; break;
85 case Instruction::SetGT: opCode = BG; break;
86 default:
87 assert(0 && "Unrecognized VM instruction!");
88 break;
89 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000090 }
91 else
92 {
93 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000094 {
95 case Instruction::SetEQ: opCode = BE; break;
96 case Instruction::SetNE: opCode = BNE; break;
97 case Instruction::SetLE: opCode = BLEU; break;
98 case Instruction::SetGE: opCode = BCC; break;
99 case Instruction::SetLT: opCode = BCS; break;
100 case Instruction::SetGT: opCode = BGU; break;
101 default:
102 assert(0 && "Unrecognized VM instruction!");
103 break;
104 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000105 }
106
107 return opCode;
108}
109
110static inline MachineOpCode
111ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000112 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000113{
114 MachineOpCode opCode = INVALID_OPCODE;
115
116 switch(setCCInstr->getOpcode())
117 {
118 case Instruction::SetEQ: opCode = FBE; break;
119 case Instruction::SetNE: opCode = FBNE; break;
120 case Instruction::SetLE: opCode = FBLE; break;
121 case Instruction::SetGE: opCode = FBGE; break;
122 case Instruction::SetLT: opCode = FBL; break;
123 case Instruction::SetGT: opCode = FBG; break;
124 default:
125 assert(0 && "Unrecognized VM instruction!");
126 break;
127 }
128
129 return opCode;
130}
131
132
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000133// Create a unique TmpInstruction for a boolean value,
134// representing the CC register used by a branch on that value.
135// For now, hack this using a little static cache of TmpInstructions.
136// Eventually the entire BURG instruction selection should be put
137// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000138// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000139// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000140//
141static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000143{
Chris Lattner09ff1122002-07-24 21:21:32 +0000144 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000145 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000146 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147
148 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
149
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000150 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000151 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000152 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000153 boolToTmpCache.clear();
154 }
155
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000156 // Look for tmpI and create a new one otherwise. The new value is
157 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000158 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
159 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000160 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000161
162 return tmpI;
163}
164
165
Chris Lattner20b1ea02001-09-14 03:47:57 +0000166static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000167ChooseBccInstruction(const InstructionNode* instrNode,
168 bool& isFPBranch)
169{
170 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000171 assert(setCCNode->getOpLabel() == SetCCOp);
172 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000173 const Type* setCCType = setCCInstr->getOperand(0)->getType();
174
Vikram S. Adve242a8082002-05-19 15:25:51 +0000175 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
176
177 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000178 return ChooseBFpccInstruction(instrNode, setCCInstr);
179 else
180 return ChooseBpccInstruction(instrNode, setCCInstr);
181}
182
183
184static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000185ChooseMovFpccInstruction(const InstructionNode* instrNode)
186{
187 MachineOpCode opCode = INVALID_OPCODE;
188
189 switch(instrNode->getInstruction()->getOpcode())
190 {
191 case Instruction::SetEQ: opCode = MOVFE; break;
192 case Instruction::SetNE: opCode = MOVFNE; break;
193 case Instruction::SetLE: opCode = MOVFLE; break;
194 case Instruction::SetGE: opCode = MOVFGE; break;
195 case Instruction::SetLT: opCode = MOVFL; break;
196 case Instruction::SetGT: opCode = MOVFG; break;
197 default:
198 assert(0 && "Unrecognized VM instruction!");
199 break;
200 }
201
202 return opCode;
203}
204
205
206// Assumes that SUBcc v1, v2 -> v3 has been executed.
207// In most cases, we want to clear v3 and then follow it by instruction
208// MOVcc 1 -> v3.
209// Set mustClearReg=false if v3 need not be cleared before conditional move.
210// Set valueToMove=0 if we want to conditionally move 0 instead of 1
211// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000212// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000213//
214static MachineOpCode
215ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000216 bool& mustClearReg,
217 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000218{
219 MachineOpCode opCode = INVALID_OPCODE;
220 mustClearReg = true;
221 valueToMove = 1;
222
223 switch(instrNode->getInstruction()->getOpcode())
224 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000225 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000226 case Instruction::SetLE: opCode = MOVLE; break;
227 case Instruction::SetGE: opCode = MOVGE; break;
228 case Instruction::SetLT: opCode = MOVL; break;
229 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000230 case Instruction::SetNE: assert(0 && "No move required!"); break;
231 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000232 }
233
234 return opCode;
235}
236
Chris Lattner20b1ea02001-09-14 03:47:57 +0000237static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000238ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000239{
240 MachineOpCode opCode = INVALID_OPCODE;
241
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000242 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243 {
244 case ToFloatTy:
245 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000246 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000247 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000248 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000249 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000250 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000251 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 break;
256
257 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000258 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
259 // Both functions should treat the integer as a 32-bit value for types
260 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000261 if (opType == Type::SByteTy || opType == Type::UByteTy ||
262 opType == Type::ShortTy || opType == Type::UShortTy ||
263 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000264 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000265 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000266 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000267 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000268 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000269 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000270 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 break;
274
275 default:
276 break;
277 }
278
279 return opCode;
280}
281
282static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000283ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000284{
285 MachineOpCode opCode = INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000286
287 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
288 && "This function should only be called for FLOAT or DOUBLE");
289
290 if (tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000292 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
293 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
294 }
295 else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
296 tid==Type::UByteTyID || tid==Type::UShortTyID)
297 {
298 opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000299 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000300 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000301 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000302 opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 }
304 else
305 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000306
Chris Lattner20b1ea02001-09-14 03:47:57 +0000307 return opCode;
308}
309
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000310MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000311CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
312 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000313{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000314 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000315 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000316 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000317}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000318
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000319// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000320// The FP value must be converted to the dest type in an FP register,
321// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000322//
323// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
324// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000325// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000326// don't have this problem.)
327// For unsigned int, we therefore have to generate the code sequence:
328//
329// if (V > (float) MAXINT) {
330// unsigned result = (unsigned) (V - (float) MAXINT);
331// result = result + (unsigned) MAXINT;
332// }
333// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000334// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000335//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000336static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000337CreateCodeToConvertFloatToInt(const TargetMachine& target,
338 Value* opVal,
339 Instruction* destI,
340 std::vector<MachineInstr*>& mvec,
341 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000342{
343 // Create a temporary to represent the FP register into which the
344 // int value will placed after conversion. The type of this temporary
345 // depends on the type of FP register to use: single-prec for a 32-bit
346 // int or smaller; double-prec for a 64-bit int.
347 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000348 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000349 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
350 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000351 mcfi.addTemp(destForCast);
352
353 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000354 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
355 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000356 mvec.push_back(M);
357
358 // Create the fpreg-to-intreg copy code
359 target.getInstrInfo().
360 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000361 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000362}
363
364
Chris Lattner20b1ea02001-09-14 03:47:57 +0000365static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000366ChooseAddInstruction(const InstructionNode* instrNode)
367{
368 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
369}
370
371
Chris Lattner20b1ea02001-09-14 03:47:57 +0000372static inline MachineInstr*
373CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000374 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000375{
Chris Lattner00dca912003-01-15 17:47:49 +0000376 return BuildMI((resultType == Type::FloatTy) ? FMOVS : FMOVD, 2)
377 .addReg(instrNode->leftChild()->getValue())
378 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000379}
380
381static inline MachineInstr*
382CreateAddConstInstruction(const InstructionNode* instrNode)
383{
384 MachineInstr* minstr = NULL;
385
386 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000387 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000388
389 // Cases worth optimizing are:
390 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
391 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
392 //
Chris Lattner9b625032002-05-06 16:15:30 +0000393 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
394 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000395 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000396 minstr = CreateMovFloatInstruction(instrNode,
397 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000398 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000399
400 return minstr;
401}
402
403
404static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000405ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000406{
407 MachineOpCode opCode = INVALID_OPCODE;
408
Chris Lattner0c4e8862002-09-03 01:08:28 +0000409 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000410 {
411 opCode = SUB;
412 }
413 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000414 switch(resultType->getPrimitiveID())
415 {
416 case Type::FloatTyID: opCode = FSUBS; break;
417 case Type::DoubleTyID: opCode = FSUBD; break;
418 default: assert(0 && "Invalid type for SUB instruction"); break;
419 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000420
421 return opCode;
422}
423
424
425static inline MachineInstr*
426CreateSubConstInstruction(const InstructionNode* instrNode)
427{
428 MachineInstr* minstr = NULL;
429
430 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000431 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000432
433 // Cases worth optimizing are:
434 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
435 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
436 //
Chris Lattner9b625032002-05-06 16:15:30 +0000437 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
438 double dval = FPC->getValue();
439 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000440 minstr = CreateMovFloatInstruction(instrNode,
441 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000442 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000443
444 return minstr;
445}
446
447
448static inline MachineOpCode
449ChooseFcmpInstruction(const InstructionNode* instrNode)
450{
451 MachineOpCode opCode = INVALID_OPCODE;
452
453 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
454 switch(operand->getType()->getPrimitiveID()) {
455 case Type::FloatTyID: opCode = FCMPS; break;
456 case Type::DoubleTyID: opCode = FCMPD; break;
457 default: assert(0 && "Invalid type for FCMP instruction"); break;
458 }
459
460 return opCode;
461}
462
463
464// Assumes that leftArg and rightArg are both cast instructions.
465//
466static inline bool
467BothFloatToDouble(const InstructionNode* instrNode)
468{
469 InstrTreeNode* leftArg = instrNode->leftChild();
470 InstrTreeNode* rightArg = instrNode->rightChild();
471 InstrTreeNode* leftArgArg = leftArg->leftChild();
472 InstrTreeNode* rightArgArg = rightArg->leftChild();
473 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
474
475 // Check if both arguments are floats cast to double
476 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000477 leftArgArg->getValue()->getType() == Type::FloatTy &&
478 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479}
480
481
482static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000483ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000484{
485 MachineOpCode opCode = INVALID_OPCODE;
486
Chris Lattner0c4e8862002-09-03 01:08:28 +0000487 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000488 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000489 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000490 switch(resultType->getPrimitiveID())
491 {
492 case Type::FloatTyID: opCode = FMULS; break;
493 case Type::DoubleTyID: opCode = FMULD; break;
494 default: assert(0 && "Invalid type for MUL instruction"); break;
495 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000496
497 return opCode;
498}
499
500
Vikram S. Adve510eec72001-11-04 21:59:14 +0000501
Chris Lattner20b1ea02001-09-14 03:47:57 +0000502static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000503CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000504 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000505{
Chris Lattner00dca912003-01-15 17:47:49 +0000506 return BuildMI(SUB, 3).addMReg(target.getRegInfo().getZeroRegNum())
507 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000508}
509
510
Vikram S. Adve242a8082002-05-19 15:25:51 +0000511// Create instruction sequence for any shift operation.
512// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
513// requires a second instruction for explicit sign-extension.
514// Note that we only have to worry about a sign-bit appearing in the
515// most significant bit of the operand after shifting (e.g., bit 32 of
516// Int or bit 16 of Short), so we do not have to worry about results
517// that are as large as a normal integer register.
518//
519static inline void
520CreateShiftInstructions(const TargetMachine& target,
521 Function* F,
522 MachineOpCode shiftOpCode,
523 Value* argVal1,
524 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000525 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000526 Instruction* destVal,
527 vector<MachineInstr*>& mvec,
528 MachineCodeForInstruction& mcfi)
529{
530 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
531 "Large shift sizes unexpected, but can be handled below: "
532 "You need to check whether or not it fits in immed field below");
533
534 // If this is a logical left shift of a type smaller than the standard
535 // integer reg. size, we have to extend the sign-bit into upper bits
536 // of dest, so we need to put the result of the SLL into a temporary.
537 //
538 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000539 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000540 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000541 && opSize < target.getTargetData().getIntegerRegize())
Vikram S. Adve242a8082002-05-19 15:25:51 +0000542 { // put SLL result into a temporary
543 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
544 mcfi.addTemp(shiftDest);
545 }
546
547 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000548 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
549 .addReg(shiftDest, MOTy::Def)
550 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
551 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000552 mvec.push_back(M);
553
554 if (shiftDest != destVal)
555 { // extend the sign-bit of the result into all upper bits of dest
556 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
557 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000558 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
559 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000560 }
561}
562
563
Vikram S. Adve74825322002-03-18 03:15:35 +0000564// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000565// create a cheaper instruction.
566// This returns the approximate cost of the instructions generated,
567// which is used to pick the cheapest when both operands are constant.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000568static inline unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000569CreateMulConstInstruction(const TargetMachine &target, Function* F,
570 Value* lval, Value* rval, Instruction* destVal,
571 vector<MachineInstr*>& mvec,
572 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000573{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000574 /* Use max. multiply cost, viz., cost of MULX */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000575 unsigned cost = target.getInstrInfo().minLatency(MULX);
576 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000577
578 Value* constOp = rval;
579 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000580 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000581
582 // Cases worth optimizing are:
583 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
584 // (2) Multiply by 2^x for integer types: replace with Shift
585 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000586 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000587
Chris Lattner0c4e8862002-09-03 01:08:28 +0000588 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000589 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590 bool isValidConst;
591 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
592 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000593 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000594 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000595 bool needNeg = false;
596 if (C < 0)
597 {
598 needNeg = true;
599 C = -C;
600 }
601
Chris Lattner00dca912003-01-15 17:47:49 +0000602 if (C == 0 || C == 1) {
603 cost = target.getInstrInfo().minLatency(ADD);
604 unsigned Zero = target.getRegInfo().getZeroRegNum();
605 MachineInstr* M;
606 if (C == 0)
607 M = BuildMI(ADD,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
608 else
609 M = BuildMI(ADD,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
610 mvec.push_back(M);
611 }
Chris Lattner36346c72002-05-19 21:20:19 +0000612 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000613 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000614 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000615 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
616 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
617 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000618 }
619
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000621 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 MachineInstr* M = CreateIntNegInstruction(target, destVal);
623 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000624 }
625 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000626 }
627 else
628 {
Chris Lattner9b625032002-05-06 16:15:30 +0000629 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 {
Chris Lattner9b625032002-05-06 16:15:30 +0000631 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000632 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000633 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000634 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000635 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
636 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Chris Lattner00dca912003-01-15 17:47:49 +0000637 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000638 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000639 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000640 }
641
Vikram S. Adve242a8082002-05-19 15:25:51 +0000642 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000643 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000644 cost = 0;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000645 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000647 }
648
649 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000650}
651
652
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000653// Does not create any instructions if we cannot exploit constant to
654// create a cheaper instruction.
655//
656static inline void
657CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000658 Function* F,
659 Value* lval, Value* rval,
660 Instruction* destVal,
661 vector<MachineInstr*>& mvec,
662 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000663{
664 Value* constOp;
665 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000666 { // both operands are constant: evaluate and "set" in dest
667 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
668 cast<Constant>(lval), cast<Constant>(rval));
669 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000670 }
671 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000672 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000673 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000674 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000675
676 // else neither is constant
677 return;
678}
679
Vikram S. Adve74825322002-03-18 03:15:35 +0000680// Return NULL if we cannot exploit constant to create a cheaper instruction
681static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000682CreateMulInstruction(const TargetMachine &target, Function* F,
683 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000684 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000685 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000686 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
687{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000688 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000689 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000690 if (mvec.size() == L)
691 { // no instructions were added so create MUL reg, reg, reg.
692 // Use FSMULD if both operands are actually floats cast to doubles.
693 // Otherwise, use the default opcode for the appropriate type.
694 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
695 ? forceMulOp
696 : ChooseMulInstructionByType(destVal->getType()));
Chris Lattner00dca912003-01-15 17:47:49 +0000697 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
698 .addRegDef(destVal));
Vikram S. Adve74825322002-03-18 03:15:35 +0000699 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000700}
701
702
Vikram S. Adve510eec72001-11-04 21:59:14 +0000703// Generate a divide instruction for Div or Rem.
704// For Rem, this assumes that the operand type will be signed if the result
705// type is signed. This is correct because they must have the same sign.
706//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000707static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000708ChooseDivInstruction(TargetMachine &target,
709 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000710{
711 MachineOpCode opCode = INVALID_OPCODE;
712
713 const Type* resultType = instrNode->getInstruction()->getType();
714
Chris Lattner0c4e8862002-09-03 01:08:28 +0000715 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000716 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000718 switch(resultType->getPrimitiveID())
719 {
720 case Type::FloatTyID: opCode = FDIVS; break;
721 case Type::DoubleTyID: opCode = FDIVD; break;
722 default: assert(0 && "Invalid type for DIV instruction"); break;
723 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000724
725 return opCode;
726}
727
728
Chris Lattner54e898e2003-01-15 19:23:34 +0000729// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve74825322002-03-18 03:15:35 +0000730static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000731CreateDivConstInstruction(TargetMachine &target,
732 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000733 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000734{
Chris Lattner54e898e2003-01-15 19:23:34 +0000735 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000736 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000737 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000738 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000739
740 Value* DestVal = instrNode->getValue();
741 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000742
743 // Cases worth optimizing are:
744 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
745 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
746 //
747 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000748
Chris Lattner0c4e8862002-09-03 01:08:28 +0000749 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000750 {
751 unsigned pow;
752 bool isValidConst;
753 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
754 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000755 {
756 bool needNeg = false;
Chris Lattner54e898e2003-01-15 19:23:34 +0000757 if (C < 0) {
758 needNeg = true;
759 C = -C;
760 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000761
Chris Lattner54e898e2003-01-15 19:23:34 +0000762 if (C == 1) {
763 mvec.push_back(BuildMI(ADD, 3).addReg(LHS).addMReg(ZeroReg)
764 .addRegDef(DestVal));
765 } else if (isPowerOf2(C, pow)) {
766 unsigned opCode= ((resultType->isSigned())
767 ? (resultType==Type::LongTy) ? SRAX : SRA
768 : (resultType==Type::LongTy) ? SRLX : SRL);
769 mvec.push_back(BuildMI(opCode, 3).addReg(LHS).addZImm(pow)
770 .addRegDef(DestVal));
771 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000772
Chris Lattner54e898e2003-01-15 19:23:34 +0000773 if (needNeg && (C == 1 || isPowerOf2(C, pow)))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000774 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Chris Lattner54e898e2003-01-15 19:23:34 +0000775 mvec.push_back(CreateIntNegInstruction(target, DestVal));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000776 }
777 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000778 }
779 else
780 {
Chris Lattner9b625032002-05-06 16:15:30 +0000781 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000782 {
Chris Lattner9b625032002-05-06 16:15:30 +0000783 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000784 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000785 {
Chris Lattner54e898e2003-01-15 19:23:34 +0000786 unsigned opCode =
787 (dval < 0) ? (resultType == Type::FloatTy? FNEGS : FNEGD)
788 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000789
Chris Lattner54e898e2003-01-15 19:23:34 +0000790 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(DestVal));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000791 }
792 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000793 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000794}
795
796
Vikram S. Adve74825322002-03-18 03:15:35 +0000797static void
798CreateCodeForVariableSizeAlloca(const TargetMachine& target,
799 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000800 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +0000801 Value* numElementsVal,
802 vector<MachineInstr*>& getMvec)
803{
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000804 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +0000805 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +0000806 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000807 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +0000808
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000809 // Enforce the alignment constraints on the stack pointer at
810 // compile time if the total size is a known constant.
811 if (isa<Constant>(numElementsVal))
812 {
813 bool isValid;
814 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
815 assert(isValid && "Unexpectedly large array dimension in alloca!");
816 int64_t total = numElem * tsize;
817 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
818 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
819 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
820 }
821 else
822 {
823 // The size is not a constant. Generate code to compute it and
824 // code to pad the size for stack alignment.
825 // Create a Value to hold the (constant) element size
826 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
827
828 // Create temporary values to hold the result of MUL, SLL, SRL
829 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
830 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
831 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
832 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
833 mcfi.addTemp(tmpProd);
834 mcfi.addTemp(tmpSLL);
835 mcfi.addTemp(tmpSRL);
836
837 // Instruction 1: mul numElements, typeSize -> tmpProd
838 // This will optimize the MUL as far as possible.
839 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
840 mcfi, INVALID_MACHINE_OPCODE);
841
842 assert(0 && "Need to insert padding instructions here!");
843
844 totalSizeVal = tmpProd;
845 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000846
847 // Get the constant offset from SP for dynamically allocated storage
848 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +0000849 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000850 bool growUp;
851 ConstantSInt* dynamicAreaOffset =
852 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000853 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +0000854 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
855
Chris Lattner54e898e2003-01-15 19:23:34 +0000856 unsigned SPReg = target.getRegInfo().getStackPointer();
857
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000858 // Instruction 2: sub %sp, totalSizeVal -> %sp
Chris Lattner54e898e2003-01-15 19:23:34 +0000859 getMvec.push_back(BuildMI(SUB, 3).addMReg(SPReg).addReg(totalSizeVal)
860 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000861
Vikram S. Adve74825322002-03-18 03:15:35 +0000862 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Chris Lattner54e898e2003-01-15 19:23:34 +0000863 getMvec.push_back(BuildMI(ADD, 3).addMReg(SPReg).addReg(dynamicAreaOffset)
864 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +0000865}
866
867
868static void
869CreateCodeForFixedSizeAlloca(const TargetMachine& target,
870 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000871 unsigned tsize,
872 unsigned numElements,
Vikram S. Adve74825322002-03-18 03:15:35 +0000873 vector<MachineInstr*>& getMvec)
874{
Vikram S. Adved3e26482002-10-13 00:18:57 +0000875 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000876 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000877 "Result value is not part of a function?");
878 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +0000879 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000880
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000881 // Check if the offset would small enough to use as an immediate in
882 // load/stores (check LDX because all load/stores have the same-size immediate
883 // field). If not, put the variable in the dynamically sized area of the
884 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000885 unsigned paddedSizeIgnored;
886 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000887 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000888 tsize * numElements);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000889 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP)) {
890 CreateCodeForVariableSizeAlloca(target, result, tsize,
891 ConstantSInt::get(Type::IntTy,numElements),
892 getMvec);
893 return;
894 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000895
896 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000897 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +0000898
899 // Create a temporary Value to hold the constant offset.
900 // This is needed because it may not fit in the immediate field.
901 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
902
903 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +0000904 unsigned FPReg = target.getRegInfo().getFramePointer();
905 getMvec.push_back(BuildMI(ADD, 3).addMReg(FPReg).addReg(offsetVal)
906 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +0000907}
908
909
Chris Lattner20b1ea02001-09-14 03:47:57 +0000910//------------------------------------------------------------------------
911// Function SetOperandsForMemInstr
912//
913// Choose addressing mode for the given load or store instruction.
914// Use [reg+reg] if it is an indexed reference, and the index offset is
915// not a constant or if it cannot fit in the offset field.
916// Use [reg+offset] in all other cases.
917//
918// This assumes that all array refs are "lowered" to one of these forms:
919// %x = load (subarray*) ptr, constant ; single constant offset
920// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
921// Generally, this should happen via strength reduction + LICM.
922// Also, strength reduction should take care of using the same register for
923// the loop index variable and an array index, when that is profitable.
924//------------------------------------------------------------------------
925
926static void
Chris Lattner54e898e2003-01-15 19:23:34 +0000927SetOperandsForMemInstr(unsigned Opcode,
928 vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +0000929 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000930 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000931{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000932 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000933 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000934 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000935 bool allConstantIndices;
936 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000937
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000938 // Now create the appropriate operands for the machine instruction.
939 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000940 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000941 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000942 MachineOperand::MachineOperandType offsetOpType =
943 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000944
Vikram S. Adve74825322002-03-18 03:15:35 +0000945 // Check if there is an index vector and if so, compute the
946 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000947 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000948 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000950 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000951
Vikram S. Adve242a8082002-05-19 15:25:51 +0000952 // If all indices are constant, compute the combined offset directly.
953 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000954 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000955 // Compute the offset value using the index vector. Create a
956 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000957 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000958 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000959 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000960 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000961 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000962 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000963 // be an array ref, and must have been lowered to a single non-zero
964 // offset. (An extra leading zero offset, if any, can be ignored.)
965 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000966 //
Chris Lattner0374b8d2002-09-11 01:21:35 +0000967 bool firstIdxIsZero =
968 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000969 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000970 && "Array refs must be lowered before Instruction Selection");
971
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000972 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000973
974 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000975 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000976 MachineCodeForInstruction::get(memInst).addTemp(addr);
977
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000978 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000979 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000980 const Type* vecType = (firstIdxIsZero
981 ? GetElementPtrInst::getIndexedType(ptrType,
982 std::vector<Value*>(1U, idxVec[0]),
983 /*AllowCompositeLeaf*/ true)
984 : ptrType);
985 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +0000986 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000987 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000988
989 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +0000990 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000991 idxVal, /* lval, not likely to be const*/
992 eltSizeVal, /* rval, likely to be constant */
993 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +0000994 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000995 INVALID_MACHINE_OPCODE);
996
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000997 assert(mulVec.size() > 0 && "No multiply code created?");
Chris Lattner54e898e2003-01-15 19:23:34 +0000998 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000999
1000 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001001 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001002 }
1003 else
1004 {
1005 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1006 smallConstOffset = 0;
1007 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001008
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001009 // For STORE:
1010 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1011 // For LOAD or GET_ELEMENT_PTR,
1012 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1013 //
1014 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001015 MachineInstr *MI;
1016 if (memInst->getOpcode() == Instruction::Store) {
1017 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1018 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1019 .addReg(ptrVal).addReg(valueForRegOffset);
1020 else
1021 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1022 .addReg(ptrVal).addSImm(smallConstOffset);
1023 } else {
1024 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1025 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1026 .addRegDef(memInst);
1027 else
1028 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1029 .addRegDef(memInst);
1030 }
1031 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001032}
1033
1034
Chris Lattner20b1ea02001-09-14 03:47:57 +00001035//
1036// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001037// in place of the use(s) of that instruction in node `parent'.
1038// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001039// Also make sure to skip over a parent who:
1040// (1) is a list node in the Burg tree, or
1041// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001042//
1043static void
1044ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001045 InstrTreeNode* parent,
1046 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001047{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001048 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1049
Chris Lattner20b1ea02001-09-14 03:47:57 +00001050 Instruction* unusedOp = treeNode->getInstruction();
1051 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001052
1053 // The parent itself may be a list node, so find the real parent instruction
1054 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1055 {
1056 parent = parent->parent();
1057 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1058 }
1059 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1060
1061 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001062 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001063
1064 // The parent's mvec would be empty if it was itself forwarded.
1065 // Recursively call ForwardOperand in that case...
1066 //
1067 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001068 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001069 assert(parent->parent() != NULL &&
1070 "Parent could not have been forwarded, yet has no instructions?");
1071 ForwardOperand(treeNode, parent->parent(), operandNum);
1072 }
1073 else
1074 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001075 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001076 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001077 MachineInstr* minstr = mvec[i];
1078 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001079 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001080 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001081 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001082 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001083 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001084 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001085 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001086
1087 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1088 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001089 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001090 minstr->implicitRefIsDefined(i),
1091 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001092 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001093 }
1094}
1095
1096
Vikram S. Adve242a8082002-05-19 15:25:51 +00001097inline bool
1098AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001099{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001100 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1101 UI != UE; ++UI)
1102 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1103 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1104 return false;
1105 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001106}
1107
Vikram S. Advefb361122001-10-22 13:36:31 +00001108//******************* Externally Visible Functions *************************/
1109
Vikram S. Advefb361122001-10-22 13:36:31 +00001110//------------------------------------------------------------------------
1111// External Function: ThisIsAChainRule
1112//
1113// Purpose:
1114// Check if a given BURG rule is a chain rule.
1115//------------------------------------------------------------------------
1116
1117extern bool
1118ThisIsAChainRule(int eruleno)
1119{
1120 switch(eruleno)
1121 {
1122 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001123 case 123:
1124 case 124:
1125 case 125:
1126 case 126:
1127 case 127:
1128 case 128:
1129 case 129:
1130 case 130:
1131 case 131:
1132 case 132:
1133 case 133:
1134 case 155:
1135 case 221:
1136 case 222:
1137 case 241:
1138 case 242:
1139 case 243:
1140 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001141 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001142 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001143 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001144
Vikram S. Advefb361122001-10-22 13:36:31 +00001145 default:
1146 return false; break;
1147 }
1148}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001149
1150
1151//------------------------------------------------------------------------
1152// External Function: GetInstructionsByRule
1153//
1154// Purpose:
1155// Choose machine instructions for the SPARC according to the
1156// patterns chosen by the BURG-generated parser.
1157//------------------------------------------------------------------------
1158
Vikram S. Adve74825322002-03-18 03:15:35 +00001159void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001160GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001161 int ruleForNode,
1162 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001163 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001164 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001165{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001166 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001167 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001168 int nextRule;
1169 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001170 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001171 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001172 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001173
1174 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001175
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001176 // If the code for this instruction was folded into the parent (user),
1177 // then do nothing!
1178 if (subtreeRoot->isFoldedIntoParent())
1179 return;
1180
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001181 //
1182 // Let's check for chain rules outside the switch so that we don't have
1183 // to duplicate the list of chain rule production numbers here again
1184 //
1185 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001186 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001187 // Chain rules have a single nonterminal on the RHS.
1188 // Get the rule that matches the RHS non-terminal and use that instead.
1189 //
1190 assert(nts[0] && ! nts[1]
1191 && "A chain rule should have only one RHS non-terminal!");
1192 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1193 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001194 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001195 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001196 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001197 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001198 switch(ruleForNode) {
1199 case 1: // stmt: Ret
1200 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001201 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001202 // for moving return value to appropriate register.
1203 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001204 // Mark the return value register as an implicit ref of
1205 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001206 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001207 ReturnInst *returnInstr =
1208 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001209 assert(returnInstr->getOpcode() == Instruction::Ret);
1210
Chris Lattner9c461082002-02-03 07:50:56 +00001211 Instruction* returnReg = new TmpInstruction(returnInstr);
1212 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001213
Chris Lattner4690e6d2003-01-15 18:11:11 +00001214 M = BuildMI(JMPLRET, 3).addReg(returnReg).addSImm(8)
1215 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001216
Vikram S. Advea995e602001-10-11 04:23:19 +00001217 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001218 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001219
Vikram S. Adve74825322002-03-18 03:15:35 +00001220 mvec.push_back(M);
Chris Lattner4690e6d2003-01-15 18:11:11 +00001221 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001222
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001223 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001224 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001225
1226 case 3: // stmt: Store(reg,reg)
1227 case 4: // stmt: Store(reg,ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00001228 SetOperandsForMemInstr(ChooseStoreInstruction(
1229 subtreeRoot->leftChild()->getValue()->getType()),
1230 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001231 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001232
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001233 case 5: // stmt: BrUncond
Chris Lattner54e898e2003-01-15 19:23:34 +00001234 {
1235 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1236 mvec.push_back(BuildMI(BA, 1).addPCDisp(BI->getSuccessor(0)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001237
Chris Lattner54e898e2003-01-15 19:23:34 +00001238 // delay slot
1239 mvec.push_back(BuildMI(NOP, 0));
1240 break;
1241 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001242
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001243 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001244 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001245 // If the constant is ZERO, we can use the branch-on-integer-register
1246 // instructions and avoid the SUBcc instruction entirely.
1247 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001248 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001249 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1250 assert(constNode &&
1251 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001252 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001253 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001254
Chris Lattner0c4e8862002-09-03 01:08:28 +00001255 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001256 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001257 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1258 && isValidConst)
1259 {
1260 // That constant is a zero after all...
1261 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001262 // Mark the setCC node so that no code is generated for it.
1263 InstructionNode* setCCNode = (InstructionNode*)
1264 subtreeRoot->leftChild();
1265 assert(setCCNode->getOpLabel() == SetCCOp);
1266 setCCNode->markFoldedIntoParent();
1267
1268 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1269
Chris Lattner54e898e2003-01-15 19:23:34 +00001270 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1271 .addReg(setCCNode->leftChild()->getValue())
1272 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001273 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001274
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001275 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001276 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001277
1278 // false branch
Chris Lattner54e898e2003-01-15 19:23:34 +00001279 mvec.push_back(BuildMI(BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280
1281 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001282 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001283 break;
1284 }
1285 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001286 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001287
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001288 case 6: // stmt: BrCond(setCC)
1289 { // bool => boolean was computed with SetCC.
1290 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001291 // If it is an integer CC, we also need to find the unique
1292 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001293 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001294 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 bool isFPBranch;
Chris Lattner54e898e2003-01-15 19:23:34 +00001296 unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001297 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1298 brInst->getParent()->getParent(),
1299 isFPBranch? Type::FloatTy : Type::IntTy);
Chris Lattner54e898e2003-01-15 19:23:34 +00001300 M = BuildMI(Opcode, 2).addCCReg(ccValue)
1301 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001302 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001303
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001304 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001305 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001306
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001307 // false branch
Chris Lattner54e898e2003-01-15 19:23:34 +00001308 mvec.push_back(BuildMI(BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001309
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001311 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001312 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001313 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001314
1315 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001316 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001317 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001318 Constant* constVal =
1319 cast<Constant>(subtreeRoot->leftChild()->getValue());
1320 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321
Chris Lattner54e898e2003-01-15 19:23:34 +00001322 M = BuildMI(BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001323 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001324 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001325
1326 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001327 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001328 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001329 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330
1331 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001332 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001333 // Just use the branch-on-integer-register instruction!
1334 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001335 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1336 M = BuildMI(BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
1337 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001338 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001339
1340 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001341 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342
1343 // false branch
Chris Lattner54e898e2003-01-15 19:23:34 +00001344 mvec.push_back(BuildMI(BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345
1346 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001347 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001348 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001349 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001350
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001351 case 9: // stmt: Switch(reg)
1352 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001353 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001354
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001355 case 10: // reg: VRegList(reg, reg)
1356 assert(0 && "VRegList should never be the topmost non-chain rule");
1357 break;
1358
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001359 case 21: // bool: Not(bool,reg): Both these are implemented as:
1360 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1361 { // First find the unary operand. It may be left or right, usually right.
1362 Value* notArg = BinaryOperator::getNotArgument(
1363 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001364 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1365 mvec.push_back(BuildMI(XNOR, 3).addReg(notArg).addMReg(ZeroReg)
1366 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001368 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001370 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001371 {
1372 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001373 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001374 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001375 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001376 }
1377
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001378 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001379 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001381 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001383 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001384 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001385 //======================================================================
1386 // Rules for integer conversions:
1387 //
1388 //--------
1389 // From ISO 1998 C++ Standard, Sec. 4.7:
1390 //
1391 // 2. If the destination type is unsigned, the resulting value is
1392 // the least unsigned integer congruent to the source integer
1393 // (modulo 2n where n is the number of bits used to represent the
1394 // unsigned type). [Note: In a two s complement representation,
1395 // this conversion is conceptual and there is no change in the
1396 // bit pattern (if there is no truncation). ]
1397 //
1398 // 3. If the destination type is signed, the value is unchanged if
1399 // it can be represented in the destination type (and bitfield width);
1400 // otherwise, the value is implementation-defined.
1401 //--------
1402 //
1403 // Since we assume 2s complement representations, this implies:
1404 //
1405 // -- if operand is smaller than destination, zero-extend or sign-extend
1406 // according to the signedness of the *operand*: source decides.
1407 // ==> we have to do nothing here!
1408 //
1409 // -- if operand is same size as or larger than destination, and the
1410 // destination is *unsigned*, zero-extend the operand: dest. decides
1411 //
1412 // -- if operand is same size as or larger than destination, and the
1413 // destination is *signed*, the choice is implementation defined:
1414 // we sign-extend the operand: i.e., again dest. decides.
1415 // Note: this matches both Sun's cc and gcc3.2.
1416 //======================================================================
1417
Vikram S. Adve242a8082002-05-19 15:25:51 +00001418 Instruction* destI = subtreeRoot->getInstruction();
1419 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001420 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001421 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001422 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001423 unsigned opSize = target.getTargetData().getTypeSize(opType);
1424 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001425 if (opSize >= destSize)
1426 { // Operand is same size as or larger than dest:
1427 // zero- or sign-extend, according to the signeddness of
1428 // the destination (see above).
1429 if (destI->getType()->isSigned())
1430 target.getInstrInfo().CreateSignExtensionInstructions(target,
1431 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1432 mvec, MachineCodeForInstruction::get(destI));
1433 else
1434 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1435 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1436 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001437 }
1438 else
1439 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001440 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001441 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001442 {
1443 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1444 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001445 if (destI->getType()->isUnsigned())
1446 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001447 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001448 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001449 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1450
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001451 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001452 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001453
1454 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001455 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001456 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001457 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001458 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001459 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001460 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001461 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001462 {
1463 Instruction* destI = subtreeRoot->getInstruction();
1464 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1465 MachineCodeForInstruction::get(destI));
1466 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001467 else
1468 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001469 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001470 }
1471
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001472 case 31: // reg: ToFloatTy(reg):
1473 case 32: // reg: ToDoubleTy(reg):
1474 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001475
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001476 // If this instruction has a parent (a user) in the tree
1477 // and the user is translated as an FsMULd instruction,
1478 // then the cast is unnecessary. So check that first.
1479 // In the future, we'll want to do the same for the FdMULq instruction,
1480 // so do the check here instead of only for ToFloatTy(reg).
1481 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001482 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001483 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001484 const MachineCodeForInstruction& mcfi =
1485 MachineCodeForInstruction::get(
1486 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1487 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1488 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001489 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001490
1491 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001492 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001493 Value* leftVal = subtreeRoot->leftChild()->getValue();
1494 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001495 MachineOpCode opCode=ChooseConvertToFloatInstr(
1496 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001497 if (opCode == INVALID_OPCODE) // no conversion needed
1498 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001499 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001500 }
1501 else
1502 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001503 // If the source operand is a non-FP type it must be
1504 // first copied from int to float register via memory!
1505 Instruction *dest = subtreeRoot->getInstruction();
1506 Value* srcForCast;
1507 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001508 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001509 {
1510 // Create a temporary to represent the FP register
1511 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001512 // The type of this temporary will determine the FP
1513 // register used: single-prec for a 32-bit int or smaller,
1514 // double-prec for a 64-bit int.
1515 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001516 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001517 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001518 Type* tmpTypeToUse =
1519 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1520 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001521 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001522 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001523 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001524
Vikram S. Adve242a8082002-05-19 15:25:51 +00001525 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001526 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001527 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001528 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001529 }
1530 else
1531 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001532
Chris Lattner54e898e2003-01-15 19:23:34 +00001533 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
Vikram S. Adve74825322002-03-18 03:15:35 +00001534 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535 }
1536 }
1537 break;
1538
1539 case 19: // reg: ToArrayTy(reg):
1540 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001541 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 break;
1543
1544 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001545 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001546 M = CreateAddConstInstruction(subtreeRoot);
1547 if (M != NULL)
1548 {
1549 mvec.push_back(M);
1550 break;
1551 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001553
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001554 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001555 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001556 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001557 break;
1558
1559 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001560 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001561 M = CreateSubConstInstruction(subtreeRoot);
1562 if (M != NULL)
1563 {
1564 mvec.push_back(M);
1565 break;
1566 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001567 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001568
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001569 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001570 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001571 Add3OperandInstr(ChooseSubInstructionByType(
1572 subtreeRoot->getInstruction()->getType()),
1573 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001574 break;
1575
1576 case 135: // reg: Mul(todouble, todouble)
1577 checkCast = true;
1578 // FALL THROUGH
1579
1580 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001581 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001582 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001583 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1584 ? FSMULD
1585 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001586 Instruction* mulInstr = subtreeRoot->getInstruction();
1587 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001588 subtreeRoot->leftChild()->getValue(),
1589 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001590 mulInstr, mvec,
1591 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001592 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001593 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001594 case 335: // reg: Mul(todouble, todoubleConst)
1595 checkCast = true;
1596 // FALL THROUGH
1597
1598 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001599 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001600 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001601 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1602 ? FSMULD
1603 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001604 Instruction* mulInstr = subtreeRoot->getInstruction();
1605 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001606 subtreeRoot->leftChild()->getValue(),
1607 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001608 mulInstr, mvec,
1609 MachineCodeForInstruction::get(mulInstr),
1610 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001611 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001612 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001613 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001614 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001615 L = mvec.size();
1616 CreateDivConstInstruction(target, subtreeRoot, mvec);
1617 if (mvec.size() > L)
1618 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001619 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001620
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001622 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001623 Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot),
1624 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 break;
1626
1627 case 37: // reg: Rem(reg, reg)
1628 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001629 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001630 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001631 Instruction* remInstr = subtreeRoot->getInstruction();
1632
Chris Lattner9c461082002-02-03 07:50:56 +00001633 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001634 subtreeRoot->leftChild()->getValue(),
1635 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001636 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001637 quot,
1638 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001639 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001640
Chris Lattner54e898e2003-01-15 19:23:34 +00001641 M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
1642 .addReg(subtreeRoot->leftChild()->getValue())
1643 .addReg(subtreeRoot->rightChild()->getValue())
1644 .addRegDef(quot);
Vikram S. Adve74825322002-03-18 03:15:35 +00001645 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001646
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001647 unsigned MulOpcode =
1648 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1649 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1650 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1651 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001652 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001653
Chris Lattner54e898e2003-01-15 19:23:34 +00001654 unsigned Opcode = ChooseSubInstructionByType(
1655 subtreeRoot->getInstruction()->getType());
1656 M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue())
1657 .addReg(prod).addRegDef(subtreeRoot->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001658 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001659 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001660 }
1661
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001662 case 38: // bool: And(bool, bool)
1663 case 238: // bool: And(bool, boolconst)
1664 case 338: // reg : BAnd(reg, reg)
1665 case 538: // reg : BAnd(reg, Constant)
Chris Lattner54e898e2003-01-15 19:23:34 +00001666 Add3OperandInstr(AND, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001667 break;
1668
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001669 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001670 case 438: // bool: BAnd(bool, bnot)
1671 { // Use the argument of NOT as the second argument!
1672 // Mark the NOT node so that no code is generated for it.
1673 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1674 Value* notArg = BinaryOperator::getNotArgument(
1675 cast<BinaryOperator>(notNode->getInstruction()));
1676 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001677 Value *LHS = subtreeRoot->leftChild()->getValue();
1678 Value *Dest = subtreeRoot->getValue();
1679 mvec.push_back(BuildMI(ANDN, 3).addReg(LHS).addReg(notArg)
1680 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001681 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001682 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001683
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001684 case 39: // bool: Or(bool, bool)
1685 case 239: // bool: Or(bool, boolconst)
1686 case 339: // reg : BOr(reg, reg)
1687 case 539: // reg : BOr(reg, Constant)
Chris Lattner54e898e2003-01-15 19:23:34 +00001688 Add3OperandInstr(OR, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 break;
1690
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001691 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001692 case 439: // bool: BOr(bool, bnot)
1693 { // Use the argument of NOT as the second argument!
1694 // Mark the NOT node so that no code is generated for it.
1695 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1696 Value* notArg = BinaryOperator::getNotArgument(
1697 cast<BinaryOperator>(notNode->getInstruction()));
1698 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001699 Value *LHS = subtreeRoot->leftChild()->getValue();
1700 Value *Dest = subtreeRoot->getValue();
1701 mvec.push_back(BuildMI(ORN, 3).addReg(LHS).addReg(notArg)
1702 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001704 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001705
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001706 case 40: // bool: Xor(bool, bool)
1707 case 240: // bool: Xor(bool, boolconst)
1708 case 340: // reg : BXor(reg, reg)
1709 case 540: // reg : BXor(reg, Constant)
Chris Lattner54e898e2003-01-15 19:23:34 +00001710 Add3OperandInstr(XOR, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001711 break;
1712
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001713 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001714 case 440: // bool: BXor(bool, bnot)
1715 { // Use the argument of NOT as the second argument!
1716 // Mark the NOT node so that no code is generated for it.
1717 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1718 Value* notArg = BinaryOperator::getNotArgument(
1719 cast<BinaryOperator>(notNode->getInstruction()));
1720 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001721 Value *LHS = subtreeRoot->leftChild()->getValue();
1722 Value *Dest = subtreeRoot->getValue();
1723 mvec.push_back(BuildMI(XNOR, 3).addReg(LHS).addReg(notArg)
1724 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001726 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727
1728 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001729 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001730 // If the SetCC was folded into the user (parent), it will be
1731 // caught above. All other cases are the same as case 42,
1732 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001733 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001734 case 42: // bool: SetCC(reg, reg):
1735 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001736 // This generates a SUBCC instruction, putting the difference in
1737 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001738 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001739 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001740 // than a branch instruction, or if it is used outside the current
1741 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001742 // computed and stored in the result register. Otherwise, discard
1743 // the difference (by using %g0) and keep only the condition code.
1744 //
1745 // To compute the boolean result in a register we use a conditional
1746 // move, unless the result of the SUBCC instruction can be used as
1747 // the bool! This assumes that zero is FALSE and any non-zero
1748 // integer is TRUE.
1749 //
1750 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1751 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001752
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001753 bool keepBoolVal = parentNode == NULL ||
1754 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001755 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001756 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1757 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1758
1759 bool mustClearReg;
1760 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001761 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001762
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001763 // Mark the 4th operand as being a CC register, and as a def
1764 // A TmpInstruction is created to represent the CC "result".
1765 // Unlike other instances of TmpInstruction, this one is used
1766 // by machine code of multiple LLVM instructions, viz.,
1767 // the SetCC and the branch. Make sure to get the same one!
1768 // Note that we do this even for FP CC registers even though they
1769 // are explicit operands, because the type of the operand
1770 // needs to be a floating point condition code, not an integer
1771 // condition code. Think of this as casting the bool result to
1772 // a FP condition code register.
1773 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001774 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001775 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001776
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001777 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1778 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001779 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001780 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001781
1782 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001783 {
1784 // Integer condition: dest. should be %g0 or an integer register.
1785 // If result must be saved but condition is not SetEQ then we need
1786 // a separate instruction to compute the bool result, so discard
1787 // result of SUBcc instruction anyway.
1788 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001789 if (keepSubVal) {
1790 M = BuildMI(SUBcc, 4).addReg(subtreeRoot->leftChild()->getValue())
1791 .addReg(subtreeRoot->rightChild()->getValue())
1792 .addRegDef(subtreeRoot->getValue())
1793 .addCCReg(tmpForCC, MOTy::Def);
1794 } else {
1795 M = BuildMI(SUBcc, 4).addReg(subtreeRoot->leftChild()->getValue())
1796 .addReg(subtreeRoot->rightChild()->getValue())
1797 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def)
1798 .addCCReg(tmpForCC, MOTy::Def);
1799 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001800 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001801
1802 if (computeBoolVal)
1803 { // recompute bool using the integer condition codes
1804 movOpCode =
1805 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1806 }
1807 }
1808 else
1809 {
1810 // FP condition: dest of FCMP should be some FCCn register
Chris Lattner54e898e2003-01-15 19:23:34 +00001811 M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
1812 .addCCReg(tmpForCC, MOTy::Def)
1813 .addReg(subtreeRoot->leftChild()->getValue())
1814 .addRegDef(subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001815 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001816
1817 if (computeBoolVal)
1818 {// recompute bool using the FP condition codes
1819 mustClearReg = true;
1820 valueToMove = 1;
1821 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1822 }
1823 }
1824
1825 if (computeBoolVal)
1826 {
1827 if (mustClearReg)
1828 {// Unconditionally set register to 0
Chris Lattner54e898e2003-01-15 19:23:34 +00001829 M = BuildMI(SETHI, 2).addZImm(0).addRegDef(setCCInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001830 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001831 }
1832
1833 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001834 // Mark the register as a use (as well as a def) because the old
1835 // value should be retained if the condition is false.
Chris Lattner54e898e2003-01-15 19:23:34 +00001836 M = BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(valueToMove)
1837 .addReg(setCCInstr, MOTy::UseAndDef);
Vikram S. Adve74825322002-03-18 03:15:35 +00001838 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001839 }
1840 break;
1841 }
1842
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001843 case 51: // reg: Load(reg)
1844 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00001845 SetOperandsForMemInstr(ChooseLoadInstruction(
1846 subtreeRoot->getValue()->getType()),
1847 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001848 break;
1849
1850 case 55: // reg: GetElemPtr(reg)
1851 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001852 // If the GetElemPtr was folded into the user (parent), it will be
1853 // caught above. For other cases, we have to compute the address.
Chris Lattner54e898e2003-01-15 19:23:34 +00001854 SetOperandsForMemInstr(ADD, mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001855 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001856
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001857 case 57: // reg: Alloca: Implement as 1 instruction:
1858 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001859 AllocationInst* instr =
1860 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001861 unsigned tsize =
1862 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001863 assert(tsize != 0);
1864 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001865 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001866 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001867
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001868 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1869 // mul num, typeSz -> tmp
1870 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001871 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001872 AllocationInst* instr =
1873 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001874 const Type* eltType = instr->getAllocatedType();
1875
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001876 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001877 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001878 Value* numElementsVal = NULL;
1879 bool isArray = instr->isArrayAllocation();
1880
1881 if (!isArray ||
1882 isa<Constant>(numElementsVal = instr->getArraySize()))
1883 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001884 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001885 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1886 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1887 numElements, mvec);
1888 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001889 else // total size is not constant.
1890 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001891 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001892 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001893 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001894
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001895 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001896 { // Generate a direct (CALL) or indirect (JMPL) call.
1897 // Mark the return-address register, the indirection
1898 // register (for indirect calls), the operands of the Call,
1899 // and the return value (if any) as implicit operands
1900 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001901 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001902 // If this is a varargs function, floating point arguments
1903 // have to passed in integer registers so insert
1904 // copy-float-to-int instructions for each float operand.
1905 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001906 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001907 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001908
1909 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00001910 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001911 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001912 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001913
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001914 // Generate the machine instruction and its operands.
1915 // Use CALL for direct function calls; this optimistically assumes
1916 // the PC-relative address fits in the CALL address field (22 bits).
1917 // Use JMPL for indirect calls.
1918 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001919 if (isa<Function>(callee)) // direct function call
Chris Lattner00dca912003-01-15 17:47:49 +00001920 M = BuildMI(CALL, 1).addPCDisp(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001921 else // indirect function call
Chris Lattner4690e6d2003-01-15 18:11:11 +00001922 M = BuildMI(JMPLCALL, 3).addReg(callee).addSImm((int64_t)0)
1923 .addRegDef(retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001924 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001925
Vikram S. Adve242a8082002-05-19 15:25:51 +00001926 const FunctionType* funcType =
1927 cast<FunctionType>(cast<PointerType>(callee->getType())
1928 ->getElementType());
1929 bool isVarArgs = funcType->isVarArg();
1930 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001931
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001932 // Use a descriptor to pass information about call arguments
1933 // to the register allocator. This descriptor will be "owned"
1934 // and freed automatically when the MachineCodeForInstruction
1935 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001936 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
1937 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00001938
Vikram S. Adve242a8082002-05-19 15:25:51 +00001939 assert(callInstr->getOperand(0) == callee
1940 && "This is assumed in the loop below!");
1941
1942 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
1943 {
1944 Value* argVal = callInstr->getOperand(i);
1945 Instruction* intArgReg = NULL;
1946
1947 // Check for FP arguments to varargs functions.
1948 // Any such argument in the first $K$ args must be passed in an
1949 // integer register, where K = #integer argument registers.
1950 if (isVarArgs && argVal->getType()->isFloatingPoint())
1951 {
1952 // If it is a function with no prototype, pass value
1953 // as an FP value as well as a varargs value
1954 if (noPrototype)
1955 argDesc->getArgInfo(i-1).setUseFPArgReg();
1956
1957 // If this arg. is in the first $K$ regs, add a copy
1958 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001959 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00001960 {
1961 MachineCodeForInstruction &destMCFI =
1962 MachineCodeForInstruction::get(callInstr);
1963 intArgReg = new TmpInstruction(Type::IntTy, argVal);
1964 destMCFI.addTemp(intArgReg);
1965
1966 vector<MachineInstr*> copyMvec;
1967 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
1968 callInstr->getParent()->getParent(),
1969 argVal, (TmpInstruction*) intArgReg,
1970 copyMvec, destMCFI);
1971 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
1972
1973 argDesc->getArgInfo(i-1).setUseIntArgReg();
1974 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
1975 }
1976 else
1977 // Cannot fit in first $K$ regs so pass the arg on the stack
1978 argDesc->getArgInfo(i-1).setUseStackSlot();
1979 }
1980
1981 if (intArgReg)
1982 mvec.back()->addImplicitRef(intArgReg);
1983
1984 mvec.back()->addImplicitRef(argVal);
1985 }
1986
1987 // Add the return value as an implicit ref. The call operands
1988 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001989 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00001990 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00001991
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001992 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00001993 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00001994 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001995
Vikram S. Adve74825322002-03-18 03:15:35 +00001996 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001997 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001998 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001999 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002000
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002001 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002002 {
2003 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2004 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2005 Instruction* shlInstr = subtreeRoot->getInstruction();
2006
2007 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002008 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2009 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002010
2011 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2012 (opType == Type::LongTy)? SLLX : SLL,
2013 argVal1, argVal2, 0, shlInstr, mvec,
2014 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002015 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002016 }
2017
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002018 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002019 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002020 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2021 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002022 Add3OperandInstr(opType->isSigned()
2023 ? (opType == Type::LongTy ? SRAX : SRA)
2024 : (opType == Type::LongTy ? SRLX : SRL),
2025 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002026 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002027 }
2028
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002029 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002030 break; // don't forward the value
2031
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002032 case 71: // reg: VReg
2033 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002034 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002035
2036 default:
2037 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002038 break;
2039 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002040 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002041
Chris Lattner20b1ea02001-09-14 03:47:57 +00002042 if (forwardOperandNum >= 0)
2043 { // We did not generate a machine instruction but need to use operand.
2044 // If user is in the same tree, replace Value in its machine operand.
2045 // If not, insert a copy instruction which should get coalesced away
2046 // by register allocation.
2047 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002048 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002049 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002050 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002051 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002052 Instruction* instr = subtreeRoot->getInstruction();
2053 target.getInstrInfo().
2054 CreateCopyInstructionsByType(target,
2055 instr->getParent()->getParent(),
2056 instr->getOperand(forwardOperandNum),
2057 instr, minstrVec,
2058 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002059 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002060 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002061 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002062 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002063
2064 if (maskUnsignedResult)
2065 { // If result is unsigned and smaller than int reg size,
2066 // we need to clear high bits of result value.
2067 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2068 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002069 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002070 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002071 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002072 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002073 { // Mask high bits. Use a TmpInstruction to represent the
2074 // intermediate result before masking. Since those instructions
2075 // have already been generated, go back and substitute tmpI
2076 // for dest in the result position of each one of them.
2077 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2078 NULL, "maskHi");
2079 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2080
2081 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2082 mvec[i]->substituteValue(dest, tmpI);
2083
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002084 M = BuildMI(SRL, 3).addReg(tmpI).addZImm(8*(4-destSize))
2085 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002086 mvec.push_back(M);
2087 }
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002088 else if (destSize < target.getTargetData().getIntegerRegize())
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002089 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002090 }
2091 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002092}