blob: 05208a4a2193c37df01e9a86be0b962777da67b8 [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
Vikram S. Adve74825322002-03-18 03:15:35 +00001254 M = new MachineInstr(JMPLRET);
Chris Lattner1c7907e2002-10-28 20:11:17 +00001255 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1256 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001257 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner1c7907e2002-10-28 20:11:17 +00001258 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001259 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001260
Vikram S. Advea995e602001-10-11 04:23:19 +00001261 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001262 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001263
Vikram S. Adve74825322002-03-18 03:15:35 +00001264 mvec.push_back(M);
1265 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001266
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001267 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001268 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001269
1270 case 3: // stmt: Store(reg,reg)
1271 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001272 mvec.push_back(new MachineInstr(
1273 ChooseStoreInstruction(
1274 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001275 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001277
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001278 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001279 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001280 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001281 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001282 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001283
1284 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001285 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001287
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001288 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001289 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001290 // If the constant is ZERO, we can use the branch-on-integer-register
1291 // instructions and avoid the SUBcc instruction entirely.
1292 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001293 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1295 assert(constNode &&
1296 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001297 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001298 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001299
Chris Lattner0c4e8862002-09-03 01:08:28 +00001300 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001301 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001302 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1303 && isValidConst)
1304 {
1305 // That constant is a zero after all...
1306 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001307 // Mark the setCC node so that no code is generated for it.
1308 InstructionNode* setCCNode = (InstructionNode*)
1309 subtreeRoot->leftChild();
1310 assert(setCCNode->getOpLabel() == SetCCOp);
1311 setCCNode->markFoldedIntoParent();
1312
1313 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1314
Vikram S. Adve74825322002-03-18 03:15:35 +00001315 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1316 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001317 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001318 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1319 brInst->getSuccessor(0));
1320 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001321
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001322 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001323 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001324
1325 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001326 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001327 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001328 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001329 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330
1331 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001332 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001333
1334 break;
1335 }
1336 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001337 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001338
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001339 case 6: // stmt: BrCond(setCC)
1340 { // bool => boolean was computed with SetCC.
1341 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001342 // If it is an integer CC, we also need to find the unique
1343 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001345 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001347 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001348
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001349 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1350 brInst->getParent()->getParent(),
1351 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001352
Vikram S. Adve74825322002-03-18 03:15:35 +00001353 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1354 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1355 brInst->getSuccessor(0));
1356 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001357
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001358 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001359 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001360
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001361 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001362 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001363 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001364 brInst->getSuccessor(1));
1365 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001366
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001368 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001370 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001371
1372 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001373 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001375 Constant* constVal =
1376 cast<Constant>(subtreeRoot->leftChild()->getValue());
1377 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001378
Vikram S. Adve74825322002-03-18 03:15:35 +00001379 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001380 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001381 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001382 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383
1384 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001385 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001386 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001387 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388
1389 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001390 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391 // Just use the branch-on-integer-register instruction!
1392 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 M = new MachineInstr(BRNZ);
1394 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001395 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001396 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001397 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001398 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399
1400 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001401 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402
1403 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001404 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001405 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001406 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001407 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001408
1409 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001410 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001411 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001412 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001413
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001414 case 9: // stmt: Switch(reg)
1415 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001416 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001417
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418 case 10: // reg: VRegList(reg, reg)
1419 assert(0 && "VRegList should never be the topmost non-chain rule");
1420 break;
1421
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001422 case 21: // bool: Not(bool,reg): Both these are implemented as:
1423 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1424 { // First find the unary operand. It may be left or right, usually right.
1425 Value* notArg = BinaryOperator::getNotArgument(
1426 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001427 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1428 mvec.push_back(BuildMI(XNOR, 3).addReg(notArg).addMReg(ZeroReg)
1429 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001431 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001433 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001434 {
1435 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001436 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001437 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001439 }
1440
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001442 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001443 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001444 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001445 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001446 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001447 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001448 //======================================================================
1449 // Rules for integer conversions:
1450 //
1451 //--------
1452 // From ISO 1998 C++ Standard, Sec. 4.7:
1453 //
1454 // 2. If the destination type is unsigned, the resulting value is
1455 // the least unsigned integer congruent to the source integer
1456 // (modulo 2n where n is the number of bits used to represent the
1457 // unsigned type). [Note: In a two s complement representation,
1458 // this conversion is conceptual and there is no change in the
1459 // bit pattern (if there is no truncation). ]
1460 //
1461 // 3. If the destination type is signed, the value is unchanged if
1462 // it can be represented in the destination type (and bitfield width);
1463 // otherwise, the value is implementation-defined.
1464 //--------
1465 //
1466 // Since we assume 2s complement representations, this implies:
1467 //
1468 // -- if operand is smaller than destination, zero-extend or sign-extend
1469 // according to the signedness of the *operand*: source decides.
1470 // ==> we have to do nothing here!
1471 //
1472 // -- if operand is same size as or larger than destination, and the
1473 // destination is *unsigned*, zero-extend the operand: dest. decides
1474 //
1475 // -- if operand is same size as or larger than destination, and the
1476 // destination is *signed*, the choice is implementation defined:
1477 // we sign-extend the operand: i.e., again dest. decides.
1478 // Note: this matches both Sun's cc and gcc3.2.
1479 //======================================================================
1480
Vikram S. Adve242a8082002-05-19 15:25:51 +00001481 Instruction* destI = subtreeRoot->getInstruction();
1482 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001483 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001484 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001485 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001486 unsigned opSize = target.getTargetData().getTypeSize(opType);
1487 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001488 if (opSize >= destSize)
1489 { // Operand is same size as or larger than dest:
1490 // zero- or sign-extend, according to the signeddness of
1491 // the destination (see above).
1492 if (destI->getType()->isSigned())
1493 target.getInstrInfo().CreateSignExtensionInstructions(target,
1494 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1495 mvec, MachineCodeForInstruction::get(destI));
1496 else
1497 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1498 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1499 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001500 }
1501 else
1502 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001503 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001504 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001505 {
1506 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1507 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001508 if (destI->getType()->isUnsigned())
1509 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001510 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001511 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001512 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1513
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001514 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001515 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001516
1517 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001518 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001519 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001520 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001521 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001522 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001523 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001524 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001525 {
1526 Instruction* destI = subtreeRoot->getInstruction();
1527 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1528 MachineCodeForInstruction::get(destI));
1529 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001530 else
1531 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001532 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001533 }
1534
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535 case 31: // reg: ToFloatTy(reg):
1536 case 32: // reg: ToDoubleTy(reg):
1537 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001538
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001539 // If this instruction has a parent (a user) in the tree
1540 // and the user is translated as an FsMULd instruction,
1541 // then the cast is unnecessary. So check that first.
1542 // In the future, we'll want to do the same for the FdMULq instruction,
1543 // so do the check here instead of only for ToFloatTy(reg).
1544 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001545 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001546 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001547 const MachineCodeForInstruction& mcfi =
1548 MachineCodeForInstruction::get(
1549 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1550 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1551 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001553
1554 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001556 Value* leftVal = subtreeRoot->leftChild()->getValue();
1557 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001558 MachineOpCode opCode=ChooseConvertToFloatInstr(
1559 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001560 if (opCode == INVALID_OPCODE) // no conversion needed
1561 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001562 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001563 }
1564 else
1565 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001566 // If the source operand is a non-FP type it must be
1567 // first copied from int to float register via memory!
1568 Instruction *dest = subtreeRoot->getInstruction();
1569 Value* srcForCast;
1570 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001571 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001572 {
1573 // Create a temporary to represent the FP register
1574 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001575 // The type of this temporary will determine the FP
1576 // register used: single-prec for a 32-bit int or smaller,
1577 // double-prec for a 64-bit int.
1578 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001579 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001580 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001581 Type* tmpTypeToUse =
1582 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1583 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001584 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001585 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001586 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001587
Vikram S. Adve242a8082002-05-19 15:25:51 +00001588 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001589 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001590 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001591 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001592 }
1593 else
1594 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001595
Vikram S. Adve74825322002-03-18 03:15:35 +00001596 M = new MachineInstr(opCode);
1597 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1598 srcForCast);
1599 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1600 dest);
1601 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001602 }
1603 }
1604 break;
1605
1606 case 19: // reg: ToArrayTy(reg):
1607 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001608 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001609 break;
1610
1611 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001612 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001613 M = CreateAddConstInstruction(subtreeRoot);
1614 if (M != NULL)
1615 {
1616 mvec.push_back(M);
1617 break;
1618 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001619 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001620
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001622 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001623 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1624 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 break;
1626
1627 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001628 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001629 M = CreateSubConstInstruction(subtreeRoot);
1630 if (M != NULL)
1631 {
1632 mvec.push_back(M);
1633 break;
1634 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001636
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001637 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001638 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001639 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1640 subtreeRoot->getInstruction()->getType())));
1641 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001642 break;
1643
1644 case 135: // reg: Mul(todouble, todouble)
1645 checkCast = true;
1646 // FALL THROUGH
1647
1648 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001649 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001650 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001651 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1652 ? FSMULD
1653 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001654 Instruction* mulInstr = subtreeRoot->getInstruction();
1655 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001656 subtreeRoot->leftChild()->getValue(),
1657 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001658 mulInstr, mvec,
1659 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001660 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001661 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001662 case 335: // reg: Mul(todouble, todoubleConst)
1663 checkCast = true;
1664 // FALL THROUGH
1665
1666 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001667 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001668 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001669 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1670 ? FSMULD
1671 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001672 Instruction* mulInstr = subtreeRoot->getInstruction();
1673 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001674 subtreeRoot->leftChild()->getValue(),
1675 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001676 mulInstr, mvec,
1677 MachineCodeForInstruction::get(mulInstr),
1678 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001680 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001681 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001682 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001683 L = mvec.size();
1684 CreateDivConstInstruction(target, subtreeRoot, mvec);
1685 if (mvec.size() > L)
1686 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001688
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001690 maskUnsignedResult = true;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001691 mvec.push_back(new MachineInstr(ChooseDivInstruction(target,
1692 subtreeRoot)));
Vikram S. Adve74825322002-03-18 03:15:35 +00001693 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 break;
1695
1696 case 37: // reg: Rem(reg, reg)
1697 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001698 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001699 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001700 Instruction* remInstr = subtreeRoot->getInstruction();
1701
Chris Lattner9c461082002-02-03 07:50:56 +00001702 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001703 subtreeRoot->leftChild()->getValue(),
1704 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001705 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001706 quot,
1707 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001708 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001709
Vikram S. Adve74825322002-03-18 03:15:35 +00001710 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1711 Set3OperandsFromInstr(M, subtreeRoot, target);
1712 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1713 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001714
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001715 unsigned MulOpcode =
1716 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1717 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1718 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1719 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001720 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001721
Vikram S. Adve74825322002-03-18 03:15:35 +00001722 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001723 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001724 Set3OperandsFromInstr(M, subtreeRoot, target);
1725 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1726 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001727
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001728 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001729 }
1730
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001731 case 38: // bool: And(bool, bool)
1732 case 238: // bool: And(bool, boolconst)
1733 case 338: // reg : BAnd(reg, reg)
1734 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001735 mvec.push_back(new MachineInstr(AND));
1736 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001737 break;
1738
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001739 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001740 case 438: // bool: BAnd(bool, bnot)
1741 { // Use the argument of NOT as the second argument!
1742 // Mark the NOT node so that no code is generated for it.
1743 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1744 Value* notArg = BinaryOperator::getNotArgument(
1745 cast<BinaryOperator>(notNode->getInstruction()));
1746 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001747 Value *LHS = subtreeRoot->leftChild()->getValue();
1748 Value *Dest = subtreeRoot->getValue();
1749 mvec.push_back(BuildMI(ANDN, 3).addReg(LHS).addReg(notArg)
1750 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001752 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001754 case 39: // bool: Or(bool, bool)
1755 case 239: // bool: Or(bool, boolconst)
1756 case 339: // reg : BOr(reg, reg)
1757 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001758 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001759 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760 break;
1761
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001762 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001763 case 439: // bool: BOr(bool, bnot)
1764 { // Use the argument of NOT as the second argument!
1765 // Mark the NOT node so that no code is generated for it.
1766 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1767 Value* notArg = BinaryOperator::getNotArgument(
1768 cast<BinaryOperator>(notNode->getInstruction()));
1769 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001770 Value *LHS = subtreeRoot->leftChild()->getValue();
1771 Value *Dest = subtreeRoot->getValue();
1772 mvec.push_back(BuildMI(ORN, 3).addReg(LHS).addReg(notArg)
1773 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001774 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001775 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001776
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001777 case 40: // bool: Xor(bool, bool)
1778 case 240: // bool: Xor(bool, boolconst)
1779 case 340: // reg : BXor(reg, reg)
1780 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001781 mvec.push_back(new MachineInstr(XOR));
1782 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001783 break;
1784
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001785 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001786 case 440: // bool: BXor(bool, bnot)
1787 { // Use the argument of NOT as the second argument!
1788 // Mark the NOT node so that no code is generated for it.
1789 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1790 Value* notArg = BinaryOperator::getNotArgument(
1791 cast<BinaryOperator>(notNode->getInstruction()));
1792 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001793 Value *LHS = subtreeRoot->leftChild()->getValue();
1794 Value *Dest = subtreeRoot->getValue();
1795 mvec.push_back(BuildMI(XNOR, 3).addReg(LHS).addReg(notArg)
1796 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001797 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001798 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001799
1800 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001801 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001802 // If the SetCC was folded into the user (parent), it will be
1803 // caught above. All other cases are the same as case 42,
1804 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001805 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001806 case 42: // bool: SetCC(reg, reg):
1807 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001808 // This generates a SUBCC instruction, putting the difference in
1809 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001810 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001811 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001812 // than a branch instruction, or if it is used outside the current
1813 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001814 // computed and stored in the result register. Otherwise, discard
1815 // the difference (by using %g0) and keep only the condition code.
1816 //
1817 // To compute the boolean result in a register we use a conditional
1818 // move, unless the result of the SUBCC instruction can be used as
1819 // the bool! This assumes that zero is FALSE and any non-zero
1820 // integer is TRUE.
1821 //
1822 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1823 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001824
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001825 bool keepBoolVal = parentNode == NULL ||
1826 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001827 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001828 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1829 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1830
1831 bool mustClearReg;
1832 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001833 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001834
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001835 // Mark the 4th operand as being a CC register, and as a def
1836 // A TmpInstruction is created to represent the CC "result".
1837 // Unlike other instances of TmpInstruction, this one is used
1838 // by machine code of multiple LLVM instructions, viz.,
1839 // the SetCC and the branch. Make sure to get the same one!
1840 // Note that we do this even for FP CC registers even though they
1841 // are explicit operands, because the type of the operand
1842 // needs to be a floating point condition code, not an integer
1843 // condition code. Think of this as casting the bool result to
1844 // a FP condition code register.
1845 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001846 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001847 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001848
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001849 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1850 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001851 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001852 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001853
1854 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001855 {
1856 // Integer condition: dest. should be %g0 or an integer register.
1857 // If result must be saved but condition is not SetEQ then we need
1858 // a separate instruction to compute the bool result, so discard
1859 // result of SUBcc instruction anyway.
1860 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001861 M = new MachineInstr(SUBcc);
1862 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1863 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1864 tmpForCC, /*def*/true);
1865 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001866
1867 if (computeBoolVal)
1868 { // recompute bool using the integer condition codes
1869 movOpCode =
1870 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1871 }
1872 }
1873 else
1874 {
1875 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001876 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1877 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001878 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001879 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001880 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001881 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001882 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001883 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001884
1885 if (computeBoolVal)
1886 {// recompute bool using the FP condition codes
1887 mustClearReg = true;
1888 valueToMove = 1;
1889 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1890 }
1891 }
1892
1893 if (computeBoolVal)
1894 {
1895 if (mustClearReg)
1896 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001897 M = new MachineInstr(SETHI);
1898 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1899 (int64_t)0);
1900 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1901 setCCInstr);
1902 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001903 }
1904
1905 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001906 // Mark the register as a use (as well as a def) because the old
1907 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001908 M = new MachineInstr(movOpCode);
1909 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1910 tmpForCC);
1911 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1912 valueToMove);
1913 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001914 setCCInstr, /*isDef*/ true,
1915 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001916 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 }
1918 break;
1919 }
1920
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001921 case 51: // reg: Load(reg)
1922 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001923 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1924 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001925 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001926 break;
1927
1928 case 55: // reg: GetElemPtr(reg)
1929 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001930 // If the GetElemPtr was folded into the user (parent), it will be
1931 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001932 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001933 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001934 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001935
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001936 case 57: // reg: Alloca: Implement as 1 instruction:
1937 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001938 AllocationInst* instr =
1939 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001940 unsigned tsize =
1941 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001942 assert(tsize != 0);
1943 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001944 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001945 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001946
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001947 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1948 // mul num, typeSz -> tmp
1949 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001950 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001951 AllocationInst* instr =
1952 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001953 const Type* eltType = instr->getAllocatedType();
1954
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001955 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001956 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001957 Value* numElementsVal = NULL;
1958 bool isArray = instr->isArrayAllocation();
1959
1960 if (!isArray ||
1961 isa<Constant>(numElementsVal = instr->getArraySize()))
1962 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001963 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001964 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1965 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1966 numElements, mvec);
1967 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001968 else // total size is not constant.
1969 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001970 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001971 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001972 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001973
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001974 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001975 { // Generate a direct (CALL) or indirect (JMPL) call.
1976 // Mark the return-address register, the indirection
1977 // register (for indirect calls), the operands of the Call,
1978 // and the return value (if any) as implicit operands
1979 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001980 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001981 // If this is a varargs function, floating point arguments
1982 // have to passed in integer registers so insert
1983 // copy-float-to-int instructions for each float operand.
1984 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001985 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001986 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001987
1988 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00001989 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001990 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001991 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001992
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001993 // Generate the machine instruction and its operands.
1994 // Use CALL for direct function calls; this optimistically assumes
1995 // the PC-relative address fits in the CALL address field (22 bits).
1996 // Use JMPL for indirect calls.
1997 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001998 if (isa<Function>(callee)) // direct function call
Chris Lattner00dca912003-01-15 17:47:49 +00001999 M = BuildMI(CALL, 1).addPCDisp(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002000 else // indirect function call
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002001 M = BuildMI(JMPLCALL,
2002 3).addReg(callee).addSImm((int64_t)0).addReg(retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00002003 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002004
Vikram S. Adve242a8082002-05-19 15:25:51 +00002005 const FunctionType* funcType =
2006 cast<FunctionType>(cast<PointerType>(callee->getType())
2007 ->getElementType());
2008 bool isVarArgs = funcType->isVarArg();
2009 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002010
Vikram S. Adveaabb5952002-10-29 19:37:31 +00002011 // Use a descriptor to pass information about call arguments
2012 // to the register allocator. This descriptor will be "owned"
2013 // and freed automatically when the MachineCodeForInstruction
2014 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002015 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2016 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00002017
Vikram S. Adve242a8082002-05-19 15:25:51 +00002018 assert(callInstr->getOperand(0) == callee
2019 && "This is assumed in the loop below!");
2020
2021 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2022 {
2023 Value* argVal = callInstr->getOperand(i);
2024 Instruction* intArgReg = NULL;
2025
2026 // Check for FP arguments to varargs functions.
2027 // Any such argument in the first $K$ args must be passed in an
2028 // integer register, where K = #integer argument registers.
2029 if (isVarArgs && argVal->getType()->isFloatingPoint())
2030 {
2031 // If it is a function with no prototype, pass value
2032 // as an FP value as well as a varargs value
2033 if (noPrototype)
2034 argDesc->getArgInfo(i-1).setUseFPArgReg();
2035
2036 // If this arg. is in the first $K$ regs, add a copy
2037 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002038 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002039 {
2040 MachineCodeForInstruction &destMCFI =
2041 MachineCodeForInstruction::get(callInstr);
2042 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2043 destMCFI.addTemp(intArgReg);
2044
2045 vector<MachineInstr*> copyMvec;
2046 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2047 callInstr->getParent()->getParent(),
2048 argVal, (TmpInstruction*) intArgReg,
2049 copyMvec, destMCFI);
2050 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2051
2052 argDesc->getArgInfo(i-1).setUseIntArgReg();
2053 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2054 }
2055 else
2056 // Cannot fit in first $K$ regs so pass the arg on the stack
2057 argDesc->getArgInfo(i-1).setUseStackSlot();
2058 }
2059
2060 if (intArgReg)
2061 mvec.back()->addImplicitRef(intArgReg);
2062
2063 mvec.back()->addImplicitRef(argVal);
2064 }
2065
2066 // Add the return value as an implicit ref. The call operands
2067 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002068 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002069 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002070
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002071 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002072 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002073 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002074
Vikram S. Adve74825322002-03-18 03:15:35 +00002075 // delay slot
2076 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002077 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002078 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002079
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002080 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002081 {
2082 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2083 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2084 Instruction* shlInstr = subtreeRoot->getInstruction();
2085
2086 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002087 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2088 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002089
2090 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2091 (opType == Type::LongTy)? SLLX : SLL,
2092 argVal1, argVal2, 0, shlInstr, mvec,
2093 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002094 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002095 }
2096
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002097 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002098 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002099 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2100 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002101 mvec.push_back(new MachineInstr((opType->isSigned()
2102 ? ((opType == Type::LongTy)? SRAX : SRA)
2103 : ((opType == Type::LongTy)? SRLX : SRL))));
2104 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002105 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002106 }
2107
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002108 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002109 break; // don't forward the value
2110
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002111 case 71: // reg: VReg
2112 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002113 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002114
2115 default:
2116 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002117 break;
2118 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002119 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002120
Chris Lattner20b1ea02001-09-14 03:47:57 +00002121 if (forwardOperandNum >= 0)
2122 { // We did not generate a machine instruction but need to use operand.
2123 // If user is in the same tree, replace Value in its machine operand.
2124 // If not, insert a copy instruction which should get coalesced away
2125 // by register allocation.
2126 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002127 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002128 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002129 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002130 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002131 Instruction* instr = subtreeRoot->getInstruction();
2132 target.getInstrInfo().
2133 CreateCopyInstructionsByType(target,
2134 instr->getParent()->getParent(),
2135 instr->getOperand(forwardOperandNum),
2136 instr, minstrVec,
2137 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002138 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002139 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002140 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002141 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002142
2143 if (maskUnsignedResult)
2144 { // If result is unsigned and smaller than int reg size,
2145 // we need to clear high bits of result value.
2146 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2147 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002148 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002149 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002150 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002151 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002152 { // Mask high bits. Use a TmpInstruction to represent the
2153 // intermediate result before masking. Since those instructions
2154 // have already been generated, go back and substitute tmpI
2155 // for dest in the result position of each one of them.
2156 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2157 NULL, "maskHi");
2158 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2159
2160 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2161 mvec[i]->substituteValue(dest, tmpI);
2162
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002163 M = BuildMI(SRL, 3).addReg(tmpI).addZImm(8*(4-destSize))
2164 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002165 mvec.push_back(M);
2166 }
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002167 else if (destSize < target.getTargetData().getIntegerRegize())
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002168 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002169 }
2170 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002171}