blob: a58aed90be0510746ab84e665ce7f70002a21517 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- InstrSelectionSupport.cpp -----------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner035dfbe2002-08-09 20:08:06 +00009//
10// Target-independent instruction selection code. See SparcInstrSelection.cpp
11// for usage.
Vikram S. Advea1d14f32001-10-10 20:50:43 +000012//
Chris Lattner035dfbe2002-08-09 20:08:06 +000013//===----------------------------------------------------------------------===//
Vikram S. Advea1d14f32001-10-10 20:50:43 +000014
15#include "llvm/CodeGen/InstrSelectionSupport.h"
16#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000017#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000019#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000021#include "llvm/Target/TargetRegInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Chris Lattner795ba6c2003-01-15 21:36:50 +000024#include "llvm/BasicBlock.h"
Chris Lattnerc5b8b1a2002-10-28 23:54:47 +000025#include "llvm/DerivedTypes.h"
Chris Lattner18153832003-07-23 14:55:59 +000026#include "../../Target/Sparc/SparcInstrSelectionSupport.h" // FIXME!
Vikram S. Advea1d14f32001-10-10 20:50:43 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
Vikram S. Advea1d14f32001-10-10 20:50:43 +000029
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000030// Generate code to load the constant into a TmpInstruction (virtual reg) and
31// returns the virtual register.
32//
Vikram S. Adve6d353262001-10-17 23:57:50 +000033static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000034InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000035 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000036 Instruction* vmInstr,
Chris Lattner18153832003-07-23 14:55:59 +000037 std::vector<MachineInstr*>& loadConstVec,
Vikram S. Adve6d353262001-10-17 23:57:50 +000038 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000039{
Vikram S. Adve6d353262001-10-17 23:57:50 +000040 // Create a tmp virtual register to hold the constant.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000041 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
Vikram S. Advef3d3ca12003-05-31 07:41:24 +000042 TmpInstruction* tmpReg = new TmpInstruction(mcfi, opValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000043
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000044 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
45 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000046
47 // Record the mapping from the tmp VM instruction to machine instruction.
48 // Do this for all machine instructions that were not mapped to any
49 // other temp values created by
50 // tmpReg->addMachineInstruction(loadConstVec.back());
51
52 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000053}
54
55
Vikram S. Advea1d14f32001-10-10 20:50:43 +000056MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +000057ChooseRegOrImmed(int64_t intValue,
58 bool isSigned,
59 MachineOpCode opCode,
60 const TargetMachine& target,
61 bool canUseImmed,
62 unsigned int& getMachineRegNum,
63 int64_t& getImmedValue)
64{
65 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
66 getMachineRegNum = 0;
67 getImmedValue = 0;
68
69 if (canUseImmed &&
Misha Brukman5e152592003-10-23 17:39:37 +000070 target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +000071 opType = isSigned? MachineOperand::MO_SignExtendedImmed
72 : MachineOperand::MO_UnextendedImmed;
73 getImmedValue = intValue;
Misha Brukman5e152592003-10-23 17:39:37 +000074 } else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) {
75 opType = MachineOperand::MO_MachineRegister;
76 getMachineRegNum = target.getRegInfo().getZeroRegNum();
77 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +000078
79 return opType;
80}
81
82
83MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +000084ChooseRegOrImmed(Value* val,
85 MachineOpCode opCode,
86 const TargetMachine& target,
87 bool canUseImmed,
88 unsigned int& getMachineRegNum,
89 int64_t& getImmedValue)
90{
Vikram S. Advea1d14f32001-10-10 20:50:43 +000091 getMachineRegNum = 0;
92 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +000093
94 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Vikram S. Adveb5161b62003-07-29 19:50:12 +000095 // TargetInstrInfo::ConvertConstantToIntType() does the right conversions:
96 bool isValidConstant;
97 uint64_t valueToUse =
98 target.getInstrInfo().ConvertConstantToIntType(target, val, val->getType(),
99 isValidConstant);
100 if (! isValidConstant)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000101 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000102
Vikram S. Adveb5161b62003-07-29 19:50:12 +0000103 // Now check if the constant value fits in the IMMED field.
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000104 //
Vikram S. Adveb5161b62003-07-29 19:50:12 +0000105 return ChooseRegOrImmed((int64_t) valueToUse, val->getType()->isSigned(),
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000106 opCode, target, canUseImmed,
107 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000108}
109
Vikram S. Adve6d353262001-10-17 23:57:50 +0000110//---------------------------------------------------------------------------
111// Function: FixConstantOperandsForInstr
112//
113// Purpose:
114// Special handling for constant operands of a machine instruction
115// -- if the constant is 0, use the hardwired 0 register, if any;
116// -- if the constant fits in the IMMEDIATE field, use that field;
117// -- else create instructions to put the constant into a register, either
118// directly or by loading explicitly from the constant pool.
119//
120// In the first 2 cases, the operand of `minstr' is modified in place.
121// Returns a vector of machine instructions generated for operands that
122// fall under case 3; these must be inserted before `minstr'.
123//---------------------------------------------------------------------------
124
Chris Lattner18153832003-07-23 14:55:59 +0000125std::vector<MachineInstr*>
Vikram S. Adve6d353262001-10-17 23:57:50 +0000126FixConstantOperandsForInstr(Instruction* vmInstr,
127 MachineInstr* minstr,
128 TargetMachine& target)
129{
Chris Lattner18153832003-07-23 14:55:59 +0000130 std::vector<MachineInstr*> MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000131
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000132 MachineOpCode opCode = minstr->getOpCode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000133 const TargetInstrInfo& instrInfo = target.getInstrInfo();
Chris Lattner8f780272002-10-29 17:25:41 +0000134 int resultPos = instrInfo.getResultPos(opCode);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000135 int immedPos = instrInfo.getImmedConstantPos(opCode);
136
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000138
Vikram S. Adve6d353262001-10-17 23:57:50 +0000139 for (unsigned op=0; op < minstr->getNumOperands(); op++)
140 {
141 const MachineOperand& mop = minstr->getOperand(op);
142
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000143 // Skip the result position, preallocated machine registers, or operands
144 // that cannot be constants (CC regs or PC-relative displacements)
Chris Lattner8f780272002-10-29 17:25:41 +0000145 if (resultPos == (int)op ||
Chris Lattner133f0792002-10-28 04:45:29 +0000146 mop.getType() == MachineOperand::MO_MachineRegister ||
147 mop.getType() == MachineOperand::MO_CCRegister ||
148 mop.getType() == MachineOperand::MO_PCRelativeDisp)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000149 continue;
150
Vikram S. Adve6d353262001-10-17 23:57:50 +0000151 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000152 unsigned int machineRegNum = 0;
153 int64_t immedValue = 0;
154 Value* opValue = NULL;
155 MachineOperand::MachineOperandType opType =
156 MachineOperand::MO_VirtualRegister;
157
158 // Operand may be a virtual register or a compile-time constant
Misha Brukman5e152592003-10-23 17:39:37 +0000159 if (mop.getType() == MachineOperand::MO_VirtualRegister) {
160 assert(mop.getVRegValue() != NULL);
161 opValue = mop.getVRegValue();
162 if (Constant *opConst = dyn_cast<Constant>(opValue)) {
163 opType = ChooseRegOrImmed(opConst, opCode, target,
164 (immedPos == (int)op), machineRegNum,
165 immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000166 if (opType == MachineOperand::MO_VirtualRegister)
Misha Brukman5e152592003-10-23 17:39:37 +0000167 constantThatMustBeLoaded = true;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000168 }
Misha Brukman5e152592003-10-23 17:39:37 +0000169 } else {
John Criswellf5ba89d2003-12-10 22:51:41 +0000170 //
171 // If the operand is from the constant pool, don't try to change it.
172 //
173 if (mop.getType() == MachineOperand::MO_ConstantPoolIndex) {
174 continue;
175 }
Misha Brukman5e152592003-10-23 17:39:37 +0000176 assert(mop.isImmediate());
177 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
178
179 // Bit-selection flags indicate an instruction that is extracting
180 // bits from its operand so ignore this even if it is a big constant.
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000181 if (mop.isHiBits32() || mop.isLoBits32() ||
182 mop.isHiBits64() || mop.isLoBits64())
Misha Brukman5e152592003-10-23 17:39:37 +0000183 continue;
184
185 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
186 opCode, target, (immedPos == (int)op),
187 machineRegNum, immedValue);
188
189 if (opType == MachineOperand::MO_SignExtendedImmed ||
190 opType == MachineOperand::MO_UnextendedImmed) {
191 // The optype is an immediate value
192 // This means we need to change the opcode, e.g. ADDr -> ADDi
193 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
194 minstr->setOpcode(newOpcode);
195 }
196
197 if (opType == mop.getType())
198 continue; // no change: this is the most common case
199
200 if (opType == MachineOperand::MO_VirtualRegister) {
201 constantThatMustBeLoaded = true;
202 opValue = isSigned
203 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
204 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
205 }
206 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000207
208 if (opType == MachineOperand::MO_MachineRegister)
209 minstr->SetMachineOperandReg(op, machineRegNum);
210 else if (opType == MachineOperand::MO_SignExtendedImmed ||
Misha Brukmanc740aae2003-06-03 03:18:20 +0000211 opType == MachineOperand::MO_UnextendedImmed) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000212 minstr->SetMachineOperandConst(op, opType, immedValue);
Misha Brukman6fe69052003-06-07 02:34:43 +0000213 // The optype is or has become an immediate
214 // This means we need to change the opcode, e.g. ADDr -> ADDi
215 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
216 minstr->setOpcode(newOpcode);
Misha Brukmanc740aae2003-06-03 03:18:20 +0000217 } else if (constantThatMustBeLoaded ||
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000218 (opValue && isa<GlobalValue>(opValue)))
219 { // opValue is a constant that must be explicitly loaded into a reg
220 assert(opValue);
221 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000222 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000223 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
224 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000225 }
226 }
227
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000228 // Also, check for implicit operands used by the machine instruction
229 // (no need to check those defined since they cannot be constants).
230 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000231 // -- arguments to a Call
232 // -- return value of a Return
233 // Any such operand that is a constant value needs to be fixed also.
234 // The current instructions with implicit refs (viz., Call and Return)
235 // have no immediate fields, so the constant always needs to be loaded
236 // into a register.
237 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000238 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000239 unsigned lastCallArgNum = 0; // unused if not a call
240 CallArgsDescriptor* argDesc = NULL; // unused if not a call
241 if (isCall)
242 argDesc = CallArgsDescriptor::get(minstr);
243
Vikram S. Adve6d353262001-10-17 23:57:50 +0000244 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000245 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000246 isa<GlobalValue>(minstr->getImplicitRef(i)))
247 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000248 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000249 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000250 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000251 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000252
Misha Brukman5e152592003-10-23 17:39:37 +0000253 if (isCall) {
254 // find and replace the argument in the CallArgsDescriptor
255 unsigned i=lastCallArgNum;
256 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
257 ++i;
258 assert(i < argDesc->getNumArgs() &&
259 "Constant operands to a call *must* be in the arg list");
260 lastCallArgNum = i;
261 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
262 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000263 }
264
Chris Lattner04120772003-01-15 19:47:53 +0000265 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000266}
Brian Gaeked0fde302003-11-11 22:41:34 +0000267
268} // End llvm namespace