blob: 1e0a82d83b2f5529e1b15abd61ace8bac710012b [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 Lattner20b1ea02001-09-14 03:47:57 +000029//************************ Internal Functions ******************************/
30
Chris Lattner20b1ea02001-09-14 03:47:57 +000031
Chris Lattner20b1ea02001-09-14 03:47:57 +000032static inline MachineOpCode
33ChooseBprInstruction(const InstructionNode* instrNode)
34{
35 MachineOpCode opCode;
36
37 Instruction* setCCInstr =
38 ((InstructionNode*) instrNode->leftChild())->getInstruction();
39
40 switch(setCCInstr->getOpcode())
41 {
42 case Instruction::SetEQ: opCode = BRZ; break;
43 case Instruction::SetNE: opCode = BRNZ; break;
44 case Instruction::SetLE: opCode = BRLEZ; break;
45 case Instruction::SetGE: opCode = BRGEZ; break;
46 case Instruction::SetLT: opCode = BRLZ; break;
47 case Instruction::SetGT: opCode = BRGZ; break;
48 default:
49 assert(0 && "Unrecognized VM instruction!");
50 opCode = INVALID_OPCODE;
51 break;
52 }
53
54 return opCode;
55}
56
57
58static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000059ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000060 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000061{
62 MachineOpCode opCode = INVALID_OPCODE;
63
64 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
65
66 if (isSigned)
67 {
68 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000069 {
70 case Instruction::SetEQ: opCode = BE; break;
71 case Instruction::SetNE: opCode = BNE; break;
72 case Instruction::SetLE: opCode = BLE; break;
73 case Instruction::SetGE: opCode = BGE; break;
74 case Instruction::SetLT: opCode = BL; break;
75 case Instruction::SetGT: opCode = BG; break;
76 default:
77 assert(0 && "Unrecognized VM instruction!");
78 break;
79 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000080 }
81 else
82 {
83 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000084 {
85 case Instruction::SetEQ: opCode = BE; break;
86 case Instruction::SetNE: opCode = BNE; break;
87 case Instruction::SetLE: opCode = BLEU; break;
88 case Instruction::SetGE: opCode = BCC; break;
89 case Instruction::SetLT: opCode = BCS; break;
90 case Instruction::SetGT: opCode = BGU; break;
91 default:
92 assert(0 && "Unrecognized VM instruction!");
93 break;
94 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000095 }
96
97 return opCode;
98}
99
100static inline MachineOpCode
101ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000102 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000103{
104 MachineOpCode opCode = INVALID_OPCODE;
105
106 switch(setCCInstr->getOpcode())
107 {
108 case Instruction::SetEQ: opCode = FBE; break;
109 case Instruction::SetNE: opCode = FBNE; break;
110 case Instruction::SetLE: opCode = FBLE; break;
111 case Instruction::SetGE: opCode = FBGE; break;
112 case Instruction::SetLT: opCode = FBL; break;
113 case Instruction::SetGT: opCode = FBG; break;
114 default:
115 assert(0 && "Unrecognized VM instruction!");
116 break;
117 }
118
119 return opCode;
120}
121
122
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000123// Create a unique TmpInstruction for a boolean value,
124// representing the CC register used by a branch on that value.
125// For now, hack this using a little static cache of TmpInstructions.
126// Eventually the entire BURG instruction selection should be put
127// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000128// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000129// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000130//
131static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000132GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000133{
Chris Lattner09ff1122002-07-24 21:21:32 +0000134 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000135 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000136 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000137
138 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
139
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000140 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000141 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000143 boolToTmpCache.clear();
144 }
145
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000146 // Look for tmpI and create a new one otherwise. The new value is
147 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000148 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
149 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000150 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000151
152 return tmpI;
153}
154
155
Chris Lattner20b1ea02001-09-14 03:47:57 +0000156static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000157ChooseBccInstruction(const InstructionNode* instrNode,
158 bool& isFPBranch)
159{
160 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000161 assert(setCCNode->getOpLabel() == SetCCOp);
162 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000163 const Type* setCCType = setCCInstr->getOperand(0)->getType();
164
Vikram S. Adve242a8082002-05-19 15:25:51 +0000165 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
166
167 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000168 return ChooseBFpccInstruction(instrNode, setCCInstr);
169 else
170 return ChooseBpccInstruction(instrNode, setCCInstr);
171}
172
173
174static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000175ChooseMovFpccInstruction(const InstructionNode* instrNode)
176{
177 MachineOpCode opCode = INVALID_OPCODE;
178
179 switch(instrNode->getInstruction()->getOpcode())
180 {
181 case Instruction::SetEQ: opCode = MOVFE; break;
182 case Instruction::SetNE: opCode = MOVFNE; break;
183 case Instruction::SetLE: opCode = MOVFLE; break;
184 case Instruction::SetGE: opCode = MOVFGE; break;
185 case Instruction::SetLT: opCode = MOVFL; break;
186 case Instruction::SetGT: opCode = MOVFG; break;
187 default:
188 assert(0 && "Unrecognized VM instruction!");
189 break;
190 }
191
192 return opCode;
193}
194
195
196// Assumes that SUBcc v1, v2 -> v3 has been executed.
197// In most cases, we want to clear v3 and then follow it by instruction
198// MOVcc 1 -> v3.
199// Set mustClearReg=false if v3 need not be cleared before conditional move.
200// Set valueToMove=0 if we want to conditionally move 0 instead of 1
201// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000202// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000203//
204static MachineOpCode
205ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000206 bool& mustClearReg,
207 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000208{
209 MachineOpCode opCode = INVALID_OPCODE;
210 mustClearReg = true;
211 valueToMove = 1;
212
213 switch(instrNode->getInstruction()->getOpcode())
214 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000215 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000216 case Instruction::SetLE: opCode = MOVLE; break;
217 case Instruction::SetGE: opCode = MOVGE; break;
218 case Instruction::SetLT: opCode = MOVL; break;
219 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000220 case Instruction::SetNE: assert(0 && "No move required!"); break;
221 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000222 }
223
224 return opCode;
225}
226
Chris Lattner20b1ea02001-09-14 03:47:57 +0000227static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000228ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000229{
230 MachineOpCode opCode = INVALID_OPCODE;
231
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000232 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000233 {
234 case ToFloatTy:
235 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000236 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000237 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000238 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000239 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000240 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000241 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000242 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000244 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000245 break;
246
247 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000248 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
249 // Both functions should treat the integer as a 32-bit value for types
250 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000251 if (opType == Type::SByteTy || opType == Type::UByteTy ||
252 opType == Type::ShortTy || opType == Type::UShortTy ||
253 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000254 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000255 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000260 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000261 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000262 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000263 break;
264
265 default:
266 break;
267 }
268
269 return opCode;
270}
271
272static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000273ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000274{
275 MachineOpCode opCode = INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000276
277 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
278 && "This function should only be called for FLOAT or DOUBLE");
279
280 if (tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000281 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000282 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
283 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
284 }
285 else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
286 tid==Type::UByteTyID || tid==Type::UShortTyID)
287 {
288 opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000290 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000292 opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000293 }
294 else
295 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000296
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 return opCode;
298}
299
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000300MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000301CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
302 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000303{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000304 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000305 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000306 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000307}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000308
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000309// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000310// The FP value must be converted to the dest type in an FP register,
311// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000312//
313// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
314// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000315// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000316// don't have this problem.)
317// For unsigned int, we therefore have to generate the code sequence:
318//
319// if (V > (float) MAXINT) {
320// unsigned result = (unsigned) (V - (float) MAXINT);
321// result = result + (unsigned) MAXINT;
322// }
323// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000324// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000325//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000326static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000327CreateCodeToConvertFloatToInt(const TargetMachine& target,
328 Value* opVal,
329 Instruction* destI,
330 std::vector<MachineInstr*>& mvec,
331 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000332{
333 // Create a temporary to represent the FP register into which the
334 // int value will placed after conversion. The type of this temporary
335 // depends on the type of FP register to use: single-prec for a 32-bit
336 // int or smaller; double-prec for a 64-bit int.
337 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000338 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000339 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
340 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000341 mcfi.addTemp(destForCast);
342
343 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000344 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
345 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000346 mvec.push_back(M);
347
348 // Create the fpreg-to-intreg copy code
349 target.getInstrInfo().
350 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000351 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000352}
353
354
Chris Lattner20b1ea02001-09-14 03:47:57 +0000355static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000356ChooseAddInstruction(const InstructionNode* instrNode)
357{
358 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
359}
360
361
Chris Lattner20b1ea02001-09-14 03:47:57 +0000362static inline MachineInstr*
363CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000364 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000365{
Chris Lattner00dca912003-01-15 17:47:49 +0000366 return BuildMI((resultType == Type::FloatTy) ? FMOVS : FMOVD, 2)
367 .addReg(instrNode->leftChild()->getValue())
368 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000369}
370
371static inline MachineInstr*
372CreateAddConstInstruction(const InstructionNode* instrNode)
373{
374 MachineInstr* minstr = NULL;
375
376 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000377 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000378
379 // Cases worth optimizing are:
380 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
381 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
382 //
Chris Lattner9b625032002-05-06 16:15:30 +0000383 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
384 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000385 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000386 minstr = CreateMovFloatInstruction(instrNode,
387 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000388 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000389
390 return minstr;
391}
392
393
394static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000395ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000396{
397 MachineOpCode opCode = INVALID_OPCODE;
398
Chris Lattner0c4e8862002-09-03 01:08:28 +0000399 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000400 {
401 opCode = SUB;
402 }
403 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000404 switch(resultType->getPrimitiveID())
405 {
406 case Type::FloatTyID: opCode = FSUBS; break;
407 case Type::DoubleTyID: opCode = FSUBD; break;
408 default: assert(0 && "Invalid type for SUB instruction"); break;
409 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000410
411 return opCode;
412}
413
414
415static inline MachineInstr*
416CreateSubConstInstruction(const InstructionNode* instrNode)
417{
418 MachineInstr* minstr = NULL;
419
420 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000421 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000422
423 // Cases worth optimizing are:
424 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
425 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
426 //
Chris Lattner9b625032002-05-06 16:15:30 +0000427 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
428 double dval = FPC->getValue();
429 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000430 minstr = CreateMovFloatInstruction(instrNode,
431 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000432 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433
434 return minstr;
435}
436
437
438static inline MachineOpCode
439ChooseFcmpInstruction(const InstructionNode* instrNode)
440{
441 MachineOpCode opCode = INVALID_OPCODE;
442
443 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
444 switch(operand->getType()->getPrimitiveID()) {
445 case Type::FloatTyID: opCode = FCMPS; break;
446 case Type::DoubleTyID: opCode = FCMPD; break;
447 default: assert(0 && "Invalid type for FCMP instruction"); break;
448 }
449
450 return opCode;
451}
452
453
454// Assumes that leftArg and rightArg are both cast instructions.
455//
456static inline bool
457BothFloatToDouble(const InstructionNode* instrNode)
458{
459 InstrTreeNode* leftArg = instrNode->leftChild();
460 InstrTreeNode* rightArg = instrNode->rightChild();
461 InstrTreeNode* leftArgArg = leftArg->leftChild();
462 InstrTreeNode* rightArgArg = rightArg->leftChild();
463 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
464
465 // Check if both arguments are floats cast to double
466 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000467 leftArgArg->getValue()->getType() == Type::FloatTy &&
468 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000469}
470
471
472static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000473ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474{
475 MachineOpCode opCode = INVALID_OPCODE;
476
Chris Lattner0c4e8862002-09-03 01:08:28 +0000477 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000478 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000480 switch(resultType->getPrimitiveID())
481 {
482 case Type::FloatTyID: opCode = FMULS; break;
483 case Type::DoubleTyID: opCode = FMULD; break;
484 default: assert(0 && "Invalid type for MUL instruction"); break;
485 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486
487 return opCode;
488}
489
490
Vikram S. Adve510eec72001-11-04 21:59:14 +0000491
Chris Lattner20b1ea02001-09-14 03:47:57 +0000492static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000493CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000494 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495{
Chris Lattner00dca912003-01-15 17:47:49 +0000496 return BuildMI(SUB, 3).addMReg(target.getRegInfo().getZeroRegNum())
497 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000498}
499
500
Vikram S. Adve242a8082002-05-19 15:25:51 +0000501// Create instruction sequence for any shift operation.
502// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
503// requires a second instruction for explicit sign-extension.
504// Note that we only have to worry about a sign-bit appearing in the
505// most significant bit of the operand after shifting (e.g., bit 32 of
506// Int or bit 16 of Short), so we do not have to worry about results
507// that are as large as a normal integer register.
508//
509static inline void
510CreateShiftInstructions(const TargetMachine& target,
511 Function* F,
512 MachineOpCode shiftOpCode,
513 Value* argVal1,
514 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000515 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000516 Instruction* destVal,
517 vector<MachineInstr*>& mvec,
518 MachineCodeForInstruction& mcfi)
519{
520 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
521 "Large shift sizes unexpected, but can be handled below: "
522 "You need to check whether or not it fits in immed field below");
523
524 // If this is a logical left shift of a type smaller than the standard
525 // integer reg. size, we have to extend the sign-bit into upper bits
526 // of dest, so we need to put the result of the SLL into a temporary.
527 //
528 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000529 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000530 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000531 && opSize < target.getTargetData().getIntegerRegize())
Vikram S. Adve242a8082002-05-19 15:25:51 +0000532 { // put SLL result into a temporary
533 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
534 mcfi.addTemp(shiftDest);
535 }
536
537 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000538 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
539 .addReg(shiftDest, MOTy::Def)
540 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
541 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000542 mvec.push_back(M);
543
544 if (shiftDest != destVal)
545 { // extend the sign-bit of the result into all upper bits of dest
546 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
547 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000548 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
549 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000550 }
551}
552
553
Vikram S. Adve74825322002-03-18 03:15:35 +0000554// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000555// create a cheaper instruction.
556// This returns the approximate cost of the instructions generated,
557// which is used to pick the cheapest when both operands are constant.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000558static inline unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000559CreateMulConstInstruction(const TargetMachine &target, Function* F,
560 Value* lval, Value* rval, Instruction* destVal,
561 vector<MachineInstr*>& mvec,
562 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000563{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000564 /* Use max. multiply cost, viz., cost of MULX */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000565 unsigned cost = target.getInstrInfo().minLatency(MULX);
566 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000567
568 Value* constOp = rval;
569 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000570 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000571
572 // Cases worth optimizing are:
573 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
574 // (2) Multiply by 2^x for integer types: replace with Shift
575 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000576 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000577
Chris Lattner0c4e8862002-09-03 01:08:28 +0000578 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000579 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000580 bool isValidConst;
581 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
582 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000583 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000584 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000585 bool needNeg = false;
586 if (C < 0)
587 {
588 needNeg = true;
589 C = -C;
590 }
591
Chris Lattner00dca912003-01-15 17:47:49 +0000592 if (C == 0 || C == 1) {
593 cost = target.getInstrInfo().minLatency(ADD);
594 unsigned Zero = target.getRegInfo().getZeroRegNum();
595 MachineInstr* M;
596 if (C == 0)
597 M = BuildMI(ADD,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
598 else
599 M = BuildMI(ADD,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
600 mvec.push_back(M);
601 }
Chris Lattner36346c72002-05-19 21:20:19 +0000602 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000603 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000604 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000605 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
606 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
607 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000608 }
609
Vikram S. Adve242a8082002-05-19 15:25:51 +0000610 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000611 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000612 MachineInstr* M = CreateIntNegInstruction(target, destVal);
613 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000614 }
615 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000616 }
617 else
618 {
Chris Lattner9b625032002-05-06 16:15:30 +0000619 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000620 {
Chris Lattner9b625032002-05-06 16:15:30 +0000621 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000622 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000623 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000624 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000625 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
626 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Chris Lattner00dca912003-01-15 17:47:49 +0000627 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000628 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000629 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000630 }
631
Vikram S. Adve242a8082002-05-19 15:25:51 +0000632 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000633 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000634 cost = 0;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000635 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000636 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000637 }
638
639 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000640}
641
642
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000643// Does not create any instructions if we cannot exploit constant to
644// create a cheaper instruction.
645//
646static inline void
647CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000648 Function* F,
649 Value* lval, Value* rval,
650 Instruction* destVal,
651 vector<MachineInstr*>& mvec,
652 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000653{
654 Value* constOp;
655 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000656 { // both operands are constant: evaluate and "set" in dest
657 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
658 cast<Constant>(lval), cast<Constant>(rval));
659 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000660 }
661 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000662 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000663 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000664 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000665
666 // else neither is constant
667 return;
668}
669
Vikram S. Adve74825322002-03-18 03:15:35 +0000670// Return NULL if we cannot exploit constant to create a cheaper instruction
671static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000672CreateMulInstruction(const TargetMachine &target, Function* F,
673 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000674 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000675 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000676 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
677{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000678 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000679 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000680 if (mvec.size() == L)
681 { // no instructions were added so create MUL reg, reg, reg.
682 // Use FSMULD if both operands are actually floats cast to doubles.
683 // Otherwise, use the default opcode for the appropriate type.
684 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
685 ? forceMulOp
686 : ChooseMulInstructionByType(destVal->getType()));
Chris Lattner00dca912003-01-15 17:47:49 +0000687 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
688 .addRegDef(destVal));
Vikram S. Adve74825322002-03-18 03:15:35 +0000689 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000690}
691
692
Vikram S. Adve510eec72001-11-04 21:59:14 +0000693// Generate a divide instruction for Div or Rem.
694// For Rem, this assumes that the operand type will be signed if the result
695// type is signed. This is correct because they must have the same sign.
696//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000697static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000698ChooseDivInstruction(TargetMachine &target,
699 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000700{
701 MachineOpCode opCode = INVALID_OPCODE;
702
703 const Type* resultType = instrNode->getInstruction()->getType();
704
Chris Lattner0c4e8862002-09-03 01:08:28 +0000705 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000706 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000707 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000708 switch(resultType->getPrimitiveID())
709 {
710 case Type::FloatTyID: opCode = FDIVS; break;
711 case Type::DoubleTyID: opCode = FDIVD; break;
712 default: assert(0 && "Invalid type for DIV instruction"); break;
713 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714
715 return opCode;
716}
717
718
Vikram S. Adve74825322002-03-18 03:15:35 +0000719// Return NULL if we cannot exploit constant to create a cheaper instruction
720static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000721CreateDivConstInstruction(TargetMachine &target,
722 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000723 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000724{
Vikram S. Adve74825322002-03-18 03:15:35 +0000725 MachineInstr* minstr1 = NULL;
726 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000727
728 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000729 if (! isa<Constant>(constOp))
730 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000731
732 // Cases worth optimizing are:
733 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
734 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
735 //
736 const Type* resultType = instrNode->getInstruction()->getType();
737
Chris Lattner0c4e8862002-09-03 01:08:28 +0000738 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000739 {
740 unsigned pow;
741 bool isValidConst;
742 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
743 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000744 {
745 bool needNeg = false;
746 if (C < 0)
747 {
748 needNeg = true;
749 C = -C;
750 }
751
752 if (C == 1)
753 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000754 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000755 minstr1->SetMachineOperandVal(0,
756 MachineOperand::MO_VirtualRegister,
757 instrNode->leftChild()->getValue());
758 minstr1->SetMachineOperandReg(1,
759 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000760 }
Chris Lattner36346c72002-05-19 21:20:19 +0000761 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000762 {
763 MachineOpCode opCode= ((resultType->isSigned())
764 ? (resultType==Type::LongTy)? SRAX : SRA
765 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000766 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000767 minstr1->SetMachineOperandVal(0,
768 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000769 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000770 minstr1->SetMachineOperandConst(1,
771 MachineOperand::MO_UnextendedImmed,
772 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000773 }
774
Vikram S. Adve74825322002-03-18 03:15:35 +0000775 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000776 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000777 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000778 instrNode->getValue());
779 }
780 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000781 }
782 else
783 {
Chris Lattner9b625032002-05-06 16:15:30 +0000784 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000785 {
Chris Lattner9b625032002-05-06 16:15:30 +0000786 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000787 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000788 {
789 bool needNeg = (dval < 0);
790
791 MachineOpCode opCode = needNeg
792 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
793 : (resultType == Type::FloatTy? FMOVS : FMOVD);
794
Vikram S. Adve74825322002-03-18 03:15:35 +0000795 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000796 minstr1->SetMachineOperandVal(0,
797 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000798 instrNode->leftChild()->getValue());
799 }
800 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000801 }
802
Vikram S. Adve74825322002-03-18 03:15:35 +0000803 if (minstr1 != NULL)
804 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
805 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000806
Vikram S. Adve74825322002-03-18 03:15:35 +0000807 if (minstr1)
808 mvec.push_back(minstr1);
809 if (minstr2)
810 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000811}
812
813
Vikram S. Adve74825322002-03-18 03:15:35 +0000814static void
815CreateCodeForVariableSizeAlloca(const TargetMachine& target,
816 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000817 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +0000818 Value* numElementsVal,
819 vector<MachineInstr*>& getMvec)
820{
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000821 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +0000822 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +0000823 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000824 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +0000825
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000826 // Enforce the alignment constraints on the stack pointer at
827 // compile time if the total size is a known constant.
828 if (isa<Constant>(numElementsVal))
829 {
830 bool isValid;
831 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
832 assert(isValid && "Unexpectedly large array dimension in alloca!");
833 int64_t total = numElem * tsize;
834 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
835 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
836 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
837 }
838 else
839 {
840 // The size is not a constant. Generate code to compute it and
841 // code to pad the size for stack alignment.
842 // Create a Value to hold the (constant) element size
843 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
844
845 // Create temporary values to hold the result of MUL, SLL, SRL
846 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
847 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
848 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
849 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
850 mcfi.addTemp(tmpProd);
851 mcfi.addTemp(tmpSLL);
852 mcfi.addTemp(tmpSRL);
853
854 // Instruction 1: mul numElements, typeSize -> tmpProd
855 // This will optimize the MUL as far as possible.
856 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
857 mcfi, INVALID_MACHINE_OPCODE);
858
859 assert(0 && "Need to insert padding instructions here!");
860
861 totalSizeVal = tmpProd;
862 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000863
864 // Get the constant offset from SP for dynamically allocated storage
865 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +0000866 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000867 bool growUp;
868 ConstantSInt* dynamicAreaOffset =
869 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000870 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +0000871 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
872
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000873 // Instruction 2: sub %sp, totalSizeVal -> %sp
Vikram S. Adve74825322002-03-18 03:15:35 +0000874 M = new MachineInstr(SUB);
875 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000876 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, totalSizeVal);
Vikram S. Adve74825322002-03-18 03:15:35 +0000877 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
878 getMvec.push_back(M);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000879
Vikram S. Adve74825322002-03-18 03:15:35 +0000880 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
881 M = new MachineInstr(ADD);
882 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000883 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
884 dynamicAreaOffset);
Vikram S. Adve74825322002-03-18 03:15:35 +0000885 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
886 getMvec.push_back(M);
887}
888
889
890static void
891CreateCodeForFixedSizeAlloca(const TargetMachine& target,
892 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000893 unsigned tsize,
894 unsigned numElements,
Vikram S. Adve74825322002-03-18 03:15:35 +0000895 vector<MachineInstr*>& getMvec)
896{
Vikram S. Adved3e26482002-10-13 00:18:57 +0000897 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000898 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000899 "Result value is not part of a function?");
900 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +0000901 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000902
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000903 // Check if the offset would small enough to use as an immediate in
904 // load/stores (check LDX because all load/stores have the same-size immediate
905 // field). If not, put the variable in the dynamically sized area of the
906 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000907 unsigned paddedSizeIgnored;
908 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000909 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000910 tsize * numElements);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000911 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP)) {
912 CreateCodeForVariableSizeAlloca(target, result, tsize,
913 ConstantSInt::get(Type::IntTy,numElements),
914 getMvec);
915 return;
916 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000917
918 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000919 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +0000920
921 // Create a temporary Value to hold the constant offset.
922 // This is needed because it may not fit in the immediate field.
923 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
924
925 // Instruction 1: add %fp, offsetFromFP -> result
926 MachineInstr* M = new MachineInstr(ADD);
927 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
928 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
929 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
930
931 getMvec.push_back(M);
932}
933
934
Chris Lattner20b1ea02001-09-14 03:47:57 +0000935//------------------------------------------------------------------------
936// Function SetOperandsForMemInstr
937//
938// Choose addressing mode for the given load or store instruction.
939// Use [reg+reg] if it is an indexed reference, and the index offset is
940// not a constant or if it cannot fit in the offset field.
941// Use [reg+offset] in all other cases.
942//
943// This assumes that all array refs are "lowered" to one of these forms:
944// %x = load (subarray*) ptr, constant ; single constant offset
945// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
946// Generally, this should happen via strength reduction + LICM.
947// Also, strength reduction should take care of using the same register for
948// the loop index variable and an array index, when that is profitable.
949//------------------------------------------------------------------------
950
951static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000952SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +0000953 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000954 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000955{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000956 Instruction* memInst = vmInstrNode->getInstruction();
957 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
958
959 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000960 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000961 bool allConstantIndices;
962 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000963
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000964 // Now create the appropriate operands for the machine instruction.
965 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000966 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000967 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000968 MachineOperand::MachineOperandType offsetOpType =
969 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000970
Vikram S. Adve74825322002-03-18 03:15:35 +0000971 // Check if there is an index vector and if so, compute the
972 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000973 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000974 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000976 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000977
Vikram S. Adve242a8082002-05-19 15:25:51 +0000978 // If all indices are constant, compute the combined offset directly.
979 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000980 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000981 // Compute the offset value using the index vector. Create a
982 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000983 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000984 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000985 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000986 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000987 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000988 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000989 // be an array ref, and must have been lowered to a single non-zero
990 // offset. (An extra leading zero offset, if any, can be ignored.)
991 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000992 //
Chris Lattner0374b8d2002-09-11 01:21:35 +0000993 bool firstIdxIsZero =
994 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000995 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000996 && "Array refs must be lowered before Instruction Selection");
997
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000998 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000999
1000 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001001 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001002 MachineCodeForInstruction::get(memInst).addTemp(addr);
1003
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001004 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001005 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001006 const Type* vecType = (firstIdxIsZero
1007 ? GetElementPtrInst::getIndexedType(ptrType,
1008 std::vector<Value*>(1U, idxVec[0]),
1009 /*AllowCompositeLeaf*/ true)
1010 : ptrType);
1011 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001012 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001013 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001014
1015 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001016 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001017 idxVal, /* lval, not likely to be const*/
1018 eltSizeVal, /* rval, likely to be constant */
1019 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001020 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001021 INVALID_MACHINE_OPCODE);
1022
1023 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1024 // to point to the same instruction it pointed to before.
1025 assert(mulVec.size() > 0 && "No multiply code created?");
1026 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1027 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1028 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1029
1030 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001031 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001032 }
1033 else
1034 {
1035 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1036 smallConstOffset = 0;
1037 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001038
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001039 // For STORE:
1040 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1041 // For LOAD or GET_ELEMENT_PTR,
1042 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1043 //
1044 unsigned offsetOpNum, ptrOpNum;
1045 if (memInst->getOpcode() == Instruction::Store)
1046 {
1047 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1048 vmInstrNode->leftChild()->getValue());
1049 ptrOpNum = 1;
1050 offsetOpNum = 2;
1051 }
1052 else
1053 {
1054 ptrOpNum = 0;
1055 offsetOpNum = 1;
1056 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1057 memInst);
1058 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001059
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001060 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1061 ptrVal);
1062
Chris Lattner20b1ea02001-09-14 03:47:57 +00001063 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1064 {
1065 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001066 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1067 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001068 }
1069 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001070 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1071 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001072}
1073
1074
Chris Lattner20b1ea02001-09-14 03:47:57 +00001075//
1076// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001077// in place of the use(s) of that instruction in node `parent'.
1078// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001079// Also make sure to skip over a parent who:
1080// (1) is a list node in the Burg tree, or
1081// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001082//
1083static void
1084ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001085 InstrTreeNode* parent,
1086 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001087{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001088 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1089
Chris Lattner20b1ea02001-09-14 03:47:57 +00001090 Instruction* unusedOp = treeNode->getInstruction();
1091 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001092
1093 // The parent itself may be a list node, so find the real parent instruction
1094 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1095 {
1096 parent = parent->parent();
1097 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1098 }
1099 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1100
1101 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001102 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001103
1104 // The parent's mvec would be empty if it was itself forwarded.
1105 // Recursively call ForwardOperand in that case...
1106 //
1107 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001108 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001109 assert(parent->parent() != NULL &&
1110 "Parent could not have been forwarded, yet has no instructions?");
1111 ForwardOperand(treeNode, parent->parent(), operandNum);
1112 }
1113 else
1114 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001115 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001116 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001117 MachineInstr* minstr = mvec[i];
1118 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001119 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001120 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001121 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001122 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001123 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001124 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001125 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001126
1127 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1128 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001129 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001130 minstr->implicitRefIsDefined(i),
1131 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001132 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001133 }
1134}
1135
1136
Vikram S. Adve242a8082002-05-19 15:25:51 +00001137inline bool
1138AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001139{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001140 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1141 UI != UE; ++UI)
1142 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1143 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1144 return false;
1145 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001146}
1147
Vikram S. Advefb361122001-10-22 13:36:31 +00001148//******************* Externally Visible Functions *************************/
1149
Vikram S. Advefb361122001-10-22 13:36:31 +00001150//------------------------------------------------------------------------
1151// External Function: ThisIsAChainRule
1152//
1153// Purpose:
1154// Check if a given BURG rule is a chain rule.
1155//------------------------------------------------------------------------
1156
1157extern bool
1158ThisIsAChainRule(int eruleno)
1159{
1160 switch(eruleno)
1161 {
1162 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001163 case 123:
1164 case 124:
1165 case 125:
1166 case 126:
1167 case 127:
1168 case 128:
1169 case 129:
1170 case 130:
1171 case 131:
1172 case 132:
1173 case 133:
1174 case 155:
1175 case 221:
1176 case 222:
1177 case 241:
1178 case 242:
1179 case 243:
1180 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001181 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001182 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001183 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001184
Vikram S. Advefb361122001-10-22 13:36:31 +00001185 default:
1186 return false; break;
1187 }
1188}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001189
1190
1191//------------------------------------------------------------------------
1192// External Function: GetInstructionsByRule
1193//
1194// Purpose:
1195// Choose machine instructions for the SPARC according to the
1196// patterns chosen by the BURG-generated parser.
1197//------------------------------------------------------------------------
1198
Vikram S. Adve74825322002-03-18 03:15:35 +00001199void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001200GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001201 int ruleForNode,
1202 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001203 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001204 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001205{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001206 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001207 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001208 int nextRule;
1209 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001210 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001211 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001212 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001213
1214 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001215
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001216 // If the code for this instruction was folded into the parent (user),
1217 // then do nothing!
1218 if (subtreeRoot->isFoldedIntoParent())
1219 return;
1220
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001221 //
1222 // Let's check for chain rules outside the switch so that we don't have
1223 // to duplicate the list of chain rule production numbers here again
1224 //
1225 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001226 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001227 // Chain rules have a single nonterminal on the RHS.
1228 // Get the rule that matches the RHS non-terminal and use that instead.
1229 //
1230 assert(nts[0] && ! nts[1]
1231 && "A chain rule should have only one RHS non-terminal!");
1232 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1233 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001234 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001235 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001236 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001237 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001238 switch(ruleForNode) {
1239 case 1: // stmt: Ret
1240 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001241 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001242 // for moving return value to appropriate register.
1243 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001244 // Mark the return value register as an implicit ref of
1245 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001246 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001247 ReturnInst *returnInstr =
1248 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001249 assert(returnInstr->getOpcode() == Instruction::Ret);
1250
Chris Lattner9c461082002-02-03 07:50:56 +00001251 Instruction* returnReg = new TmpInstruction(returnInstr);
1252 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001253
Chris Lattner4690e6d2003-01-15 18:11:11 +00001254 M = BuildMI(JMPLRET, 3).addReg(returnReg).addSImm(8)
1255 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001256
Vikram S. Advea995e602001-10-11 04:23:19 +00001257 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001258 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001259
Vikram S. Adve74825322002-03-18 03:15:35 +00001260 mvec.push_back(M);
Chris Lattner4690e6d2003-01-15 18:11:11 +00001261 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001262
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001263 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001264 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265
1266 case 3: // stmt: Store(reg,reg)
1267 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001268 mvec.push_back(new MachineInstr(
1269 ChooseStoreInstruction(
1270 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001271 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001272 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001273
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001275 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001276 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001277 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001278 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001279
1280 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001281 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001282 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001283
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001284 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001285 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 // If the constant is ZERO, we can use the branch-on-integer-register
1287 // instructions and avoid the SUBcc instruction entirely.
1288 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001289 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001290 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1291 assert(constNode &&
1292 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001293 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001295
Chris Lattner0c4e8862002-09-03 01:08:28 +00001296 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001297 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001298 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1299 && isValidConst)
1300 {
1301 // That constant is a zero after all...
1302 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001303 // Mark the setCC node so that no code is generated for it.
1304 InstructionNode* setCCNode = (InstructionNode*)
1305 subtreeRoot->leftChild();
1306 assert(setCCNode->getOpLabel() == SetCCOp);
1307 setCCNode->markFoldedIntoParent();
1308
1309 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1310
Vikram S. Adve74825322002-03-18 03:15:35 +00001311 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1312 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001313 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001314 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1315 brInst->getSuccessor(0));
1316 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001317
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001318 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001319 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001320
1321 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001322 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001323 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001324 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001325 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001326
1327 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001328 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001329
1330 break;
1331 }
1332 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001333 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001335 case 6: // stmt: BrCond(setCC)
1336 { // bool => boolean was computed with SetCC.
1337 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001338 // If it is an integer CC, we also need to find the unique
1339 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001340 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001341 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001343 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001344
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001345 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1346 brInst->getParent()->getParent(),
1347 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001348
Vikram S. Adve74825322002-03-18 03:15:35 +00001349 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1350 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1351 brInst->getSuccessor(0));
1352 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001353
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001355 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001356
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001357 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001358 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001359 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001360 brInst->getSuccessor(1));
1361 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001362
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001363 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001364 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001365 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001366 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367
1368 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001369 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001370 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001371 Constant* constVal =
1372 cast<Constant>(subtreeRoot->leftChild()->getValue());
1373 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374
Vikram S. Adve74825322002-03-18 03:15:35 +00001375 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001376 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001377 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001378 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001379
1380 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001381 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001383 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001384
1385 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001386 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001387 // Just use the branch-on-integer-register instruction!
1388 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001389 M = new MachineInstr(BRNZ);
1390 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001392 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001393 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001394 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001395
1396 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001397 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001398
1399 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001400 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001401 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001402 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001403 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001404
1405 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001406 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001408 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001409
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410 case 9: // stmt: Switch(reg)
1411 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001412 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001413
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001414 case 10: // reg: VRegList(reg, reg)
1415 assert(0 && "VRegList should never be the topmost non-chain rule");
1416 break;
1417
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001418 case 21: // bool: Not(bool,reg): Both these are implemented as:
1419 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1420 { // First find the unary operand. It may be left or right, usually right.
1421 Value* notArg = BinaryOperator::getNotArgument(
1422 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001423 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1424 mvec.push_back(BuildMI(XNOR, 3).addReg(notArg).addMReg(ZeroReg)
1425 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001427 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001428
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001429 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001430 {
1431 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001432 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001433 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001434 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001435 }
1436
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001438 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001439 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001440 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001442 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001443 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001444 //======================================================================
1445 // Rules for integer conversions:
1446 //
1447 //--------
1448 // From ISO 1998 C++ Standard, Sec. 4.7:
1449 //
1450 // 2. If the destination type is unsigned, the resulting value is
1451 // the least unsigned integer congruent to the source integer
1452 // (modulo 2n where n is the number of bits used to represent the
1453 // unsigned type). [Note: In a two s complement representation,
1454 // this conversion is conceptual and there is no change in the
1455 // bit pattern (if there is no truncation). ]
1456 //
1457 // 3. If the destination type is signed, the value is unchanged if
1458 // it can be represented in the destination type (and bitfield width);
1459 // otherwise, the value is implementation-defined.
1460 //--------
1461 //
1462 // Since we assume 2s complement representations, this implies:
1463 //
1464 // -- if operand is smaller than destination, zero-extend or sign-extend
1465 // according to the signedness of the *operand*: source decides.
1466 // ==> we have to do nothing here!
1467 //
1468 // -- if operand is same size as or larger than destination, and the
1469 // destination is *unsigned*, zero-extend the operand: dest. decides
1470 //
1471 // -- if operand is same size as or larger than destination, and the
1472 // destination is *signed*, the choice is implementation defined:
1473 // we sign-extend the operand: i.e., again dest. decides.
1474 // Note: this matches both Sun's cc and gcc3.2.
1475 //======================================================================
1476
Vikram S. Adve242a8082002-05-19 15:25:51 +00001477 Instruction* destI = subtreeRoot->getInstruction();
1478 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001479 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001480 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001481 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001482 unsigned opSize = target.getTargetData().getTypeSize(opType);
1483 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001484 if (opSize >= destSize)
1485 { // Operand is same size as or larger than dest:
1486 // zero- or sign-extend, according to the signeddness of
1487 // the destination (see above).
1488 if (destI->getType()->isSigned())
1489 target.getInstrInfo().CreateSignExtensionInstructions(target,
1490 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1491 mvec, MachineCodeForInstruction::get(destI));
1492 else
1493 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1494 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1495 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001496 }
1497 else
1498 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001499 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001500 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001501 {
1502 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1503 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001504 if (destI->getType()->isUnsigned())
1505 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001506 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001507 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001508 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1509
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001510 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001511 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001512
1513 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001514 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001515 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001516 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001517 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001518 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001519 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001520 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001521 {
1522 Instruction* destI = subtreeRoot->getInstruction();
1523 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1524 MachineCodeForInstruction::get(destI));
1525 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001526 else
1527 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001528 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001529 }
1530
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001531 case 31: // reg: ToFloatTy(reg):
1532 case 32: // reg: ToDoubleTy(reg):
1533 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001534
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535 // If this instruction has a parent (a user) in the tree
1536 // and the user is translated as an FsMULd instruction,
1537 // then the cast is unnecessary. So check that first.
1538 // In the future, we'll want to do the same for the FdMULq instruction,
1539 // so do the check here instead of only for ToFloatTy(reg).
1540 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001541 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001543 const MachineCodeForInstruction& mcfi =
1544 MachineCodeForInstruction::get(
1545 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1546 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1547 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001548 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001549
1550 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001551 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001552 Value* leftVal = subtreeRoot->leftChild()->getValue();
1553 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001554 MachineOpCode opCode=ChooseConvertToFloatInstr(
1555 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001556 if (opCode == INVALID_OPCODE) // no conversion needed
1557 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001558 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001559 }
1560 else
1561 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001562 // If the source operand is a non-FP type it must be
1563 // first copied from int to float register via memory!
1564 Instruction *dest = subtreeRoot->getInstruction();
1565 Value* srcForCast;
1566 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001567 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001568 {
1569 // Create a temporary to represent the FP register
1570 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001571 // The type of this temporary will determine the FP
1572 // register used: single-prec for a 32-bit int or smaller,
1573 // double-prec for a 64-bit int.
1574 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001575 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001576 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001577 Type* tmpTypeToUse =
1578 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1579 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001580 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001581 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001582 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001583
Vikram S. Adve242a8082002-05-19 15:25:51 +00001584 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001585 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001586 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001587 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001588 }
1589 else
1590 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001591
Vikram S. Adve74825322002-03-18 03:15:35 +00001592 M = new MachineInstr(opCode);
1593 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1594 srcForCast);
1595 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1596 dest);
1597 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001598 }
1599 }
1600 break;
1601
1602 case 19: // reg: ToArrayTy(reg):
1603 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001604 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605 break;
1606
1607 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001608 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001609 M = CreateAddConstInstruction(subtreeRoot);
1610 if (M != NULL)
1611 {
1612 mvec.push_back(M);
1613 break;
1614 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001615 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001616
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001618 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001619 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1620 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 break;
1622
1623 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001624 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001625 M = CreateSubConstInstruction(subtreeRoot);
1626 if (M != NULL)
1627 {
1628 mvec.push_back(M);
1629 break;
1630 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001631 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001632
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001634 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001635 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1636 subtreeRoot->getInstruction()->getType())));
1637 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001638 break;
1639
1640 case 135: // reg: Mul(todouble, todouble)
1641 checkCast = true;
1642 // FALL THROUGH
1643
1644 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001645 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001646 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001647 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1648 ? FSMULD
1649 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001650 Instruction* mulInstr = subtreeRoot->getInstruction();
1651 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001652 subtreeRoot->leftChild()->getValue(),
1653 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001654 mulInstr, mvec,
1655 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001656 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001657 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658 case 335: // reg: Mul(todouble, todoubleConst)
1659 checkCast = true;
1660 // FALL THROUGH
1661
1662 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001663 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001664 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1666 ? FSMULD
1667 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001668 Instruction* mulInstr = subtreeRoot->getInstruction();
1669 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001670 subtreeRoot->leftChild()->getValue(),
1671 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001672 mulInstr, mvec,
1673 MachineCodeForInstruction::get(mulInstr),
1674 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001675 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001676 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001678 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001679 L = mvec.size();
1680 CreateDivConstInstruction(target, subtreeRoot, mvec);
1681 if (mvec.size() > L)
1682 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001683 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001684
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001686 maskUnsignedResult = true;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001687 mvec.push_back(new MachineInstr(ChooseDivInstruction(target,
1688 subtreeRoot)));
Vikram S. Adve74825322002-03-18 03:15:35 +00001689 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001690 break;
1691
1692 case 37: // reg: Rem(reg, reg)
1693 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001694 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001695 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001696 Instruction* remInstr = subtreeRoot->getInstruction();
1697
Chris Lattner9c461082002-02-03 07:50:56 +00001698 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001699 subtreeRoot->leftChild()->getValue(),
1700 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001701 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001702 quot,
1703 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001704 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001705
Vikram S. Adve74825322002-03-18 03:15:35 +00001706 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1707 Set3OperandsFromInstr(M, subtreeRoot, target);
1708 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1709 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001710
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001711 unsigned MulOpcode =
1712 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1713 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1714 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1715 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001716 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001717
Vikram S. Adve74825322002-03-18 03:15:35 +00001718 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001719 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001720 Set3OperandsFromInstr(M, subtreeRoot, target);
1721 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1722 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001723
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001724 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001725 }
1726
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001727 case 38: // bool: And(bool, bool)
1728 case 238: // bool: And(bool, boolconst)
1729 case 338: // reg : BAnd(reg, reg)
1730 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001731 mvec.push_back(new MachineInstr(AND));
1732 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001733 break;
1734
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001735 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001736 case 438: // bool: BAnd(bool, bnot)
1737 { // Use the argument of NOT as the second argument!
1738 // Mark the NOT node so that no code is generated for it.
1739 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1740 Value* notArg = BinaryOperator::getNotArgument(
1741 cast<BinaryOperator>(notNode->getInstruction()));
1742 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001743 Value *LHS = subtreeRoot->leftChild()->getValue();
1744 Value *Dest = subtreeRoot->getValue();
1745 mvec.push_back(BuildMI(ANDN, 3).addReg(LHS).addReg(notArg)
1746 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001747 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001748 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001749
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001750 case 39: // bool: Or(bool, bool)
1751 case 239: // bool: Or(bool, boolconst)
1752 case 339: // reg : BOr(reg, reg)
1753 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001754 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001755 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001756 break;
1757
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001758 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001759 case 439: // bool: BOr(bool, bnot)
1760 { // Use the argument of NOT as the second argument!
1761 // Mark the NOT node so that no code is generated for it.
1762 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1763 Value* notArg = BinaryOperator::getNotArgument(
1764 cast<BinaryOperator>(notNode->getInstruction()));
1765 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001766 Value *LHS = subtreeRoot->leftChild()->getValue();
1767 Value *Dest = subtreeRoot->getValue();
1768 mvec.push_back(BuildMI(ORN, 3).addReg(LHS).addReg(notArg)
1769 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001770 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001771 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001772
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001773 case 40: // bool: Xor(bool, bool)
1774 case 240: // bool: Xor(bool, boolconst)
1775 case 340: // reg : BXor(reg, reg)
1776 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001777 mvec.push_back(new MachineInstr(XOR));
1778 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001779 break;
1780
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001781 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001782 case 440: // bool: BXor(bool, bnot)
1783 { // Use the argument of NOT as the second argument!
1784 // Mark the NOT node so that no code is generated for it.
1785 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1786 Value* notArg = BinaryOperator::getNotArgument(
1787 cast<BinaryOperator>(notNode->getInstruction()));
1788 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001789 Value *LHS = subtreeRoot->leftChild()->getValue();
1790 Value *Dest = subtreeRoot->getValue();
1791 mvec.push_back(BuildMI(XNOR, 3).addReg(LHS).addReg(notArg)
1792 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001794 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001795
1796 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001797 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001798 // If the SetCC was folded into the user (parent), it will be
1799 // caught above. All other cases are the same as case 42,
1800 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001801 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001802 case 42: // bool: SetCC(reg, reg):
1803 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001804 // This generates a SUBCC instruction, putting the difference in
1805 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001806 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001807 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001808 // than a branch instruction, or if it is used outside the current
1809 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001810 // computed and stored in the result register. Otherwise, discard
1811 // the difference (by using %g0) and keep only the condition code.
1812 //
1813 // To compute the boolean result in a register we use a conditional
1814 // move, unless the result of the SUBCC instruction can be used as
1815 // the bool! This assumes that zero is FALSE and any non-zero
1816 // integer is TRUE.
1817 //
1818 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1819 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001820
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001821 bool keepBoolVal = parentNode == NULL ||
1822 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001823 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001824 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1825 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1826
1827 bool mustClearReg;
1828 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001829 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001830
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001831 // Mark the 4th operand as being a CC register, and as a def
1832 // A TmpInstruction is created to represent the CC "result".
1833 // Unlike other instances of TmpInstruction, this one is used
1834 // by machine code of multiple LLVM instructions, viz.,
1835 // the SetCC and the branch. Make sure to get the same one!
1836 // Note that we do this even for FP CC registers even though they
1837 // are explicit operands, because the type of the operand
1838 // needs to be a floating point condition code, not an integer
1839 // condition code. Think of this as casting the bool result to
1840 // a FP condition code register.
1841 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001842 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001843 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001844
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001845 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1846 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001847 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001848 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001849
1850 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001851 {
1852 // Integer condition: dest. should be %g0 or an integer register.
1853 // If result must be saved but condition is not SetEQ then we need
1854 // a separate instruction to compute the bool result, so discard
1855 // result of SUBcc instruction anyway.
1856 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001857 M = new MachineInstr(SUBcc);
1858 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1859 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1860 tmpForCC, /*def*/true);
1861 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001862
1863 if (computeBoolVal)
1864 { // recompute bool using the integer condition codes
1865 movOpCode =
1866 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1867 }
1868 }
1869 else
1870 {
1871 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001872 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1873 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001874 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001875 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001876 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001877 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001878 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001879 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001880
1881 if (computeBoolVal)
1882 {// recompute bool using the FP condition codes
1883 mustClearReg = true;
1884 valueToMove = 1;
1885 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1886 }
1887 }
1888
1889 if (computeBoolVal)
1890 {
1891 if (mustClearReg)
1892 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001893 M = new MachineInstr(SETHI);
1894 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1895 (int64_t)0);
1896 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1897 setCCInstr);
1898 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001899 }
1900
1901 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001902 // Mark the register as a use (as well as a def) because the old
1903 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001904 M = new MachineInstr(movOpCode);
1905 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1906 tmpForCC);
1907 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1908 valueToMove);
1909 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001910 setCCInstr, /*isDef*/ true,
1911 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001912 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001913 }
1914 break;
1915 }
1916
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 case 51: // reg: Load(reg)
1918 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001919 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1920 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001921 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001922 break;
1923
1924 case 55: // reg: GetElemPtr(reg)
1925 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001926 // If the GetElemPtr was folded into the user (parent), it will be
1927 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001928 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001929 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001930 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001931
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001932 case 57: // reg: Alloca: Implement as 1 instruction:
1933 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001934 AllocationInst* instr =
1935 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001936 unsigned tsize =
1937 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001938 assert(tsize != 0);
1939 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001940 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001941 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001942
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001943 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1944 // mul num, typeSz -> tmp
1945 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001946 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001947 AllocationInst* instr =
1948 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001949 const Type* eltType = instr->getAllocatedType();
1950
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001951 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001952 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001953 Value* numElementsVal = NULL;
1954 bool isArray = instr->isArrayAllocation();
1955
1956 if (!isArray ||
1957 isa<Constant>(numElementsVal = instr->getArraySize()))
1958 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001959 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001960 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1961 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1962 numElements, mvec);
1963 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001964 else // total size is not constant.
1965 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001966 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001967 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001968 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001969
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001970 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001971 { // Generate a direct (CALL) or indirect (JMPL) call.
1972 // Mark the return-address register, the indirection
1973 // register (for indirect calls), the operands of the Call,
1974 // and the return value (if any) as implicit operands
1975 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001976 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001977 // If this is a varargs function, floating point arguments
1978 // have to passed in integer registers so insert
1979 // copy-float-to-int instructions for each float operand.
1980 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001981 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001982 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001983
1984 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00001985 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001986 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001987 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001988
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001989 // Generate the machine instruction and its operands.
1990 // Use CALL for direct function calls; this optimistically assumes
1991 // the PC-relative address fits in the CALL address field (22 bits).
1992 // Use JMPL for indirect calls.
1993 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001994 if (isa<Function>(callee)) // direct function call
Chris Lattner00dca912003-01-15 17:47:49 +00001995 M = BuildMI(CALL, 1).addPCDisp(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001996 else // indirect function call
Chris Lattner4690e6d2003-01-15 18:11:11 +00001997 M = BuildMI(JMPLCALL, 3).addReg(callee).addSImm((int64_t)0)
1998 .addRegDef(retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001999 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002000
Vikram S. Adve242a8082002-05-19 15:25:51 +00002001 const FunctionType* funcType =
2002 cast<FunctionType>(cast<PointerType>(callee->getType())
2003 ->getElementType());
2004 bool isVarArgs = funcType->isVarArg();
2005 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002006
Vikram S. Adveaabb5952002-10-29 19:37:31 +00002007 // Use a descriptor to pass information about call arguments
2008 // to the register allocator. This descriptor will be "owned"
2009 // and freed automatically when the MachineCodeForInstruction
2010 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002011 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2012 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00002013
Vikram S. Adve242a8082002-05-19 15:25:51 +00002014 assert(callInstr->getOperand(0) == callee
2015 && "This is assumed in the loop below!");
2016
2017 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2018 {
2019 Value* argVal = callInstr->getOperand(i);
2020 Instruction* intArgReg = NULL;
2021
2022 // Check for FP arguments to varargs functions.
2023 // Any such argument in the first $K$ args must be passed in an
2024 // integer register, where K = #integer argument registers.
2025 if (isVarArgs && argVal->getType()->isFloatingPoint())
2026 {
2027 // If it is a function with no prototype, pass value
2028 // as an FP value as well as a varargs value
2029 if (noPrototype)
2030 argDesc->getArgInfo(i-1).setUseFPArgReg();
2031
2032 // If this arg. is in the first $K$ regs, add a copy
2033 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002034 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002035 {
2036 MachineCodeForInstruction &destMCFI =
2037 MachineCodeForInstruction::get(callInstr);
2038 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2039 destMCFI.addTemp(intArgReg);
2040
2041 vector<MachineInstr*> copyMvec;
2042 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2043 callInstr->getParent()->getParent(),
2044 argVal, (TmpInstruction*) intArgReg,
2045 copyMvec, destMCFI);
2046 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2047
2048 argDesc->getArgInfo(i-1).setUseIntArgReg();
2049 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2050 }
2051 else
2052 // Cannot fit in first $K$ regs so pass the arg on the stack
2053 argDesc->getArgInfo(i-1).setUseStackSlot();
2054 }
2055
2056 if (intArgReg)
2057 mvec.back()->addImplicitRef(intArgReg);
2058
2059 mvec.back()->addImplicitRef(argVal);
2060 }
2061
2062 // Add the return value as an implicit ref. The call operands
2063 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002064 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002065 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002066
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002067 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002068 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002069 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002070
Vikram S. Adve74825322002-03-18 03:15:35 +00002071 // delay slot
2072 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002073 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002074 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002075
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002076 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002077 {
2078 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2079 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2080 Instruction* shlInstr = subtreeRoot->getInstruction();
2081
2082 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002083 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2084 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002085
2086 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2087 (opType == Type::LongTy)? SLLX : SLL,
2088 argVal1, argVal2, 0, shlInstr, mvec,
2089 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002090 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002091 }
2092
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002093 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002094 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002095 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2096 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002097 mvec.push_back(new MachineInstr((opType->isSigned()
2098 ? ((opType == Type::LongTy)? SRAX : SRA)
2099 : ((opType == Type::LongTy)? SRLX : SRL))));
2100 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002101 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002102 }
2103
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002104 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002105 break; // don't forward the value
2106
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002107 case 71: // reg: VReg
2108 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002109 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002110
2111 default:
2112 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002113 break;
2114 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002115 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002116
Chris Lattner20b1ea02001-09-14 03:47:57 +00002117 if (forwardOperandNum >= 0)
2118 { // We did not generate a machine instruction but need to use operand.
2119 // If user is in the same tree, replace Value in its machine operand.
2120 // If not, insert a copy instruction which should get coalesced away
2121 // by register allocation.
2122 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002123 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002124 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002125 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002126 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002127 Instruction* instr = subtreeRoot->getInstruction();
2128 target.getInstrInfo().
2129 CreateCopyInstructionsByType(target,
2130 instr->getParent()->getParent(),
2131 instr->getOperand(forwardOperandNum),
2132 instr, minstrVec,
2133 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002134 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002135 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002136 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002137 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002138
2139 if (maskUnsignedResult)
2140 { // If result is unsigned and smaller than int reg size,
2141 // we need to clear high bits of result value.
2142 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2143 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002144 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002145 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002146 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002147 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002148 { // Mask high bits. Use a TmpInstruction to represent the
2149 // intermediate result before masking. Since those instructions
2150 // have already been generated, go back and substitute tmpI
2151 // for dest in the result position of each one of them.
2152 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2153 NULL, "maskHi");
2154 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2155
2156 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2157 mvec[i]->substituteValue(dest, tmpI);
2158
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002159 M = BuildMI(SRL, 3).addReg(tmpI).addZImm(8*(4-destSize))
2160 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002161 mvec.push_back(M);
2162 }
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002163 else if (destSize < target.getTargetData().getIntegerRegize())
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002164 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002165 }
2166 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002167}