blob: fd1fdc329e1436e05a34c6b1353baaca1003ce8d [file] [log] [blame]
Brian Gaeke03cac372004-04-25 07:04:49 +00001//===- SparcV9PreSelection.cpp - Specialize LLVM code for SparcV9 ---------===//
John Criswell482202a2003-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//===----------------------------------------------------------------------===//
Vikram S. Advee9ac29b2002-09-20 00:29:28 +00009//
Brian Gaeke03cac372004-04-25 07:04:49 +000010// This file defines the PreSelection pass which specializes LLVM code for
11// the SparcV9 instruction selector, while remaining in legal portable LLVM
12// form and preserving type information and type safety. This is meant to enable
13// dataflow optimizations on SparcV9-specific operations such as accesses to
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000014// constants, globals, and array indexing.
15//
16//===----------------------------------------------------------------------===//
17
Brian Gaeke94e95d22004-02-25 18:44:15 +000018#include "SparcV9Internals.h"
Brian Gaeke4b621da2004-08-04 07:29:40 +000019#include "SparcV9BurgISel.h"
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000020#include "llvm/Constants.h"
Misha Brukman980d74c2003-10-22 04:51:36 +000021#include "llvm/DerivedTypes.h"
Chris Lattner66a64fb2004-07-29 17:11:37 +000022#include "llvm/Instructions.h"
Misha Brukman88be7002003-10-22 03:27:45 +000023#include "llvm/Module.h"
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000024#include "llvm/Pass.h"
Misha Brukman88be7002003-10-22 03:27:45 +000025#include "llvm/Support/InstVisitor.h"
Chris Lattnerdfcf8e32004-04-04 20:44:05 +000026#include "llvm/Support/GetElementPtrTypeIterator.h"
Misha Brukman88be7002003-10-22 03:27:45 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Transforms/Scalar.h"
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +000030#include <algorithm>
Chris Lattnerdfcf8e32004-04-04 20:44:05 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000033namespace {
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000034
35 //===--------------------------------------------------------------------===//
Brian Gaeke03cac372004-04-25 07:04:49 +000036 // PreSelection Pass - Specialize LLVM code for the SparcV9 instr. selector.
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000037 //
Misha Brukman81c748c2003-11-07 17:31:22 +000038 class PreSelection : public FunctionPass, public InstVisitor<PreSelection> {
Vikram S. Adve82dca372003-06-05 21:12:56 +000039 const TargetInstrInfo &instrInfo;
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000040
41 public:
Chris Lattner2e2a0ed2003-10-21 14:49:19 +000042 PreSelection(const TargetMachine &T)
Chris Lattner82baa9c2004-06-02 05:55:25 +000043 : instrInfo(*T.getInstrInfo()) {}
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000044
Misha Brukman81c748c2003-11-07 17:31:22 +000045 // runOnFunction - apply this pass to each Function
46 bool runOnFunction(Function &F) {
47 visit(F);
Chris Lattner2e2a0ed2003-10-21 14:49:19 +000048 return true;
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000049 }
Brian Gaekec0289102004-03-11 19:23:15 +000050 const char *getPassName() const { return "SparcV9 Instr. Pre-selection"; }
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000051
52 // These methods do the actual work of specializing code
53 void visitInstruction(Instruction &I); // common work for every instr.
54 void visitGetElementPtrInst(GetElementPtrInst &I);
Vikram S. Adveba6f8e22003-05-31 07:34:57 +000055 void visitCallInst(CallInst &I);
Chris Lattner4439aee2003-10-21 16:09:23 +000056 void visitPHINode(PHINode &PN);
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000057
Chris Lattnerea9a85a2004-10-16 18:14:10 +000058 void visitBasicBlock(BasicBlock &BB) {
59 if (isa<UnreachableInst>(BB.getTerminator())) {
60 BB.getInstList().pop_back();
61 const Type *RetTy = BB.getParent()->getReturnType();
62 Value *RetVal = RetTy == Type::VoidTy ? 0 : UndefValue::get(RetTy);
63 new ReturnInst(RetVal, &BB);
64 }
65 }
66
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000067 // Helper functions for visiting operands of every instruction
Vikram S. Adveba6f8e22003-05-31 07:34:57 +000068 //
69 // visitOperands() works on every operand in [firstOp, lastOp-1].
70 // If lastOp==0, lastOp defaults to #operands or #incoming Phi values.
71 //
72 // visitOneOperand() does all the work for one operand.
73 //
Chris Lattner898a42a2003-10-21 16:06:07 +000074 void visitOperands(Instruction &I, int firstOp=0);
Vikram S. Adveba6f8e22003-05-31 07:34:57 +000075 void visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
76 Instruction& insertBefore);
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000077 };
Chris Lattner13cafd02003-04-24 18:35:51 +000078
Misha Brukman81c748c2003-11-07 17:31:22 +000079#if 0
Chris Lattner13cafd02003-04-24 18:35:51 +000080 // Register the pass...
Misha Brukman81c748c2003-11-07 17:31:22 +000081 RegisterPass<PreSelection> X("preselect",
82 "Specialize LLVM code for a target machine"
83 createPreselectionPass);
84#endif
Brian Gaeke960707c2003-11-11 22:41:34 +000085
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000086} // end anonymous namespace
87
88
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +000089//------------------------------------------------------------------------------
90// Helper functions used by methods of class PreSelection
91//------------------------------------------------------------------------------
92
93
94// getGlobalAddr(): Put address of a global into a v. register.
Misha Brukman88be7002003-10-22 03:27:45 +000095static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) {
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +000096
Chris Lattner46666cf2003-06-04 01:24:40 +000097 return (isa<GlobalVariable>(ptr))
Vikram S. Advee9ac29b2002-09-20 00:29:28 +000098 ? new GetElementPtrInst(ptr,
99 std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)),
Brian Gaekeae6fb8a2004-06-23 21:41:32 +0000100 "addrOfGlobal:" + ptr->getName(), &insertBefore)
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000101 : NULL;
102}
103
Misha Brukman426275b2003-12-17 22:06:08 +0000104// Wrapper on Constant::classof to use in find_if
Misha Brukman88be7002003-10-22 03:27:45 +0000105inline static bool nonConstant(const Use& U) {
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000106 return ! isa<Constant>(U);
107}
108
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000109static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
110 Instruction& insertBefore)
111{
112 Value *getArg1, *getArg2;
Brian Gaekecc244112004-04-02 17:52:29 +0000113
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000114 switch(CE->getOpcode())
115 {
116 case Instruction::Cast:
117 getArg1 = CE->getOperand(0);
118 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
119 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
120 return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore);
121
122 case Instruction::GetElementPtr:
Chris Lattnerfb340042002-10-27 19:09:51 +0000123 assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end()
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000124 && "All indices in ConstantExpr getelementptr must be constant!");
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000125 getArg1 = CE->getOperand(0);
126 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
127 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
128 else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore))
129 getArg1 = gep;
130 return new GetElementPtrInst(getArg1,
Chris Lattnerfb340042002-10-27 19:09:51 +0000131 std::vector<Value*>(CE->op_begin()+1, CE->op_end()),
Brian Gaekeae6fb8a2004-06-23 21:41:32 +0000132 "constantGEP:" + getArg1->getName(), &insertBefore);
Brian Gaekecc244112004-04-02 17:52:29 +0000133
134 case Instruction::Select: {
135 Value *C, *S1, *S2;
136 C = CE->getOperand (0);
137 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr> (C))
138 C = DecomposeConstantExpr (CEarg, insertBefore);
139 S1 = CE->getOperand (1);
140 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr> (S1))
141 S1 = DecomposeConstantExpr (CEarg, insertBefore);
142 S2 = CE->getOperand (2);
143 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr> (S2))
144 S2 = DecomposeConstantExpr (CEarg, insertBefore);
Brian Gaeke89313452004-04-07 18:31:47 +0000145 return new SelectInst (C, S1, S2, "constantSelect", &insertBefore);
Brian Gaekecc244112004-04-02 17:52:29 +0000146 }
147
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000148 default: // must be a binary operator
Chris Lattner69ce8672002-10-13 19:39:16 +0000149 assert(CE->getOpcode() >= Instruction::BinaryOpsBegin &&
150 CE->getOpcode() < Instruction::BinaryOpsEnd &&
Brian Gaekecc244112004-04-02 17:52:29 +0000151 "Unhandled opcode in ConstantExpr");
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000152 getArg1 = CE->getOperand(0);
153 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
154 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
155 getArg2 = CE->getOperand(1);
156 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
157 getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
158 return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(),
159 getArg1, getArg2,
160 "constantBinaryOp", &insertBefore);
161 }
162}
163
Brian Gaeke28f75c22004-07-27 21:11:20 +0000164static inline bool ConstantTypeMustBeLoaded(const Type* CVT) {
165 assert(CVT->isPrimitiveType() || isa<PointerType>(CVT));
166 return !(CVT->isIntegral() || isa<PointerType>(CVT));
167}
Vikram S. Adve4ef6cce2002-10-13 00:01:57 +0000168
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000169//------------------------------------------------------------------------------
170// Instruction visitor methods to perform instruction-specific operations
171//------------------------------------------------------------------------------
Vikram S. Adve82dca372003-06-05 21:12:56 +0000172inline void
173PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
174 Instruction& insertBefore)
175{
Vikram S. Advecf952cb2003-07-02 01:23:15 +0000176 assert(&insertBefore != NULL && "Must have instruction to insert before.");
177
Vikram S. Adve82dca372003-06-05 21:12:56 +0000178 if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) {
179 I.setOperand(opNum, gep); // replace global operand
Vikram S. Advecf952cb2003-07-02 01:23:15 +0000180 return; // nothing more to do for this op.
Vikram S. Adve82dca372003-06-05 21:12:56 +0000181 }
182
183 Constant* CV = dyn_cast<Constant>(Op);
184 if (CV == NULL)
185 return;
186
Misha Brukman88be7002003-10-22 03:27:45 +0000187 if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
188 // load-time constant: factor it out so we optimize as best we can
189 Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore);
190 I.setOperand(opNum, computeConst); // replace expr operand with result
Brian Gaeke28f75c22004-07-27 21:11:20 +0000191 } else if (ConstantTypeMustBeLoaded(CV->getType())) {
Misha Brukman88be7002003-10-22 03:27:45 +0000192 // load address of constant into a register, then load the constant
Misha Brukman81c748c2003-11-07 17:31:22 +0000193 // this is now done during instruction selection
194 // the constant will live in the MachineConstantPool later on
Brian Gaeke64f51df2004-07-27 17:43:23 +0000195 } else if (ConstantMayNotFitInImmedField(CV, &I)) {
Misha Brukman88be7002003-10-22 03:27:45 +0000196 // put the constant into a virtual register using a cast
197 CastInst* castI = new CastInst(CV, CV->getType(), "copyConst",
198 &insertBefore);
199 I.setOperand(opNum, castI); // replace operand with copy in v.reg.
200 }
Vikram S. Adve82dca372003-06-05 21:12:56 +0000201}
202
Misha Brukman426275b2003-12-17 22:06:08 +0000203/// visitOperands - transform individual operands of all instructions:
204/// -- Load "large" int constants into a virtual register. What is large
205/// depends on the type of instruction and on the target architecture.
206/// -- For any constants that cannot be put in an immediate field,
207/// load address into virtual register first, and then load the constant.
208///
209/// firstOp and lastOp can be used to skip leading and trailing operands.
210/// If lastOp is 0, it defaults to #operands or #incoming Phi values.
211///
Chris Lattner898a42a2003-10-21 16:06:07 +0000212inline void PreSelection::visitOperands(Instruction &I, int firstOp) {
Vikram S. Adve82dca372003-06-05 21:12:56 +0000213 // For any instruction other than PHI, copies go just before the instr.
Chris Lattner4439aee2003-10-21 16:09:23 +0000214 for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i)
215 visitOneOperand(I, I.getOperand(i), i, I);
216}
217
218
219void PreSelection::visitPHINode(PHINode &PN) {
Vikram S. Adve82dca372003-06-05 21:12:56 +0000220 // For a PHI, operand copies must be before the terminator of the
221 // appropriate predecessor basic block. Remaining logic is simple
222 // so just handle PHIs and other instructions separately.
223 //
Chris Lattner4439aee2003-10-21 16:09:23 +0000224 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner73d93552003-10-21 17:22:23 +0000225 visitOneOperand(PN, PN.getIncomingValue(i),
Chris Lattner4439aee2003-10-21 16:09:23 +0000226 PN.getOperandNumForIncomingValue(i),
227 *PN.getIncomingBlock(i)->getTerminator());
228 // do not call visitOperands!
Vikram S. Adve82dca372003-06-05 21:12:56 +0000229}
230
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000231// Common work for *all* instructions. This needs to be called explicitly
232// by other visit<InstructionType> functions.
Misha Brukman88be7002003-10-22 03:27:45 +0000233inline void PreSelection::visitInstruction(Instruction &I) {
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000234 visitOperands(I); // Perform operand transformations
235}
236
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000237// GetElementPtr instructions: check if pointer is a global
Misha Brukman88be7002003-10-22 03:27:45 +0000238void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) {
Vikram S. Advecf952cb2003-07-02 01:23:15 +0000239 Instruction* curI = &I;
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000240
Chris Lattnerdfcf8e32004-04-04 20:44:05 +0000241 // The Sparc backend doesn't handle array indexes that are not long types, so
242 // insert a cast from whatever it is to long, if the sequential type index is
243 // not a long already.
244 unsigned Idx = 1;
245 for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I); TI != E;
246 ++TI, ++Idx)
247 if (isa<SequentialType>(*TI) &&
248 I.getOperand(Idx)->getType() != Type::LongTy) {
249 Value *Op = I.getOperand(Idx);
250 if (Op->getType()->isUnsigned()) // Must sign extend!
251 Op = new CastInst(Op, Op->getType()->getSignedVersion(), "v9", &I);
252 if (Op->getType() != Type::LongTy)
253 Op = new CastInst(Op, Type::LongTy, "v9", &I);
254 I.setOperand(Idx, Op);
255 }
256
257
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000258 // Decompose multidimensional array references
Vikram S. Advecf952cb2003-07-02 01:23:15 +0000259 if (I.getNumIndices() >= 2) {
260 // DecomposeArrayRef() replaces I and deletes it, if successful,
261 // so remember predecessor in order to find the replacement instruction.
262 // Also remember the basic block in case there is no predecessor.
263 Instruction* prevI = I.getPrev();
264 BasicBlock* bb = I.getParent();
265 if (DecomposeArrayRef(&I))
266 // first instr. replacing I
267 curI = cast<GetElementPtrInst>(prevI? prevI->getNext() : &bb->front());
268 }
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000269
270 // Perform other transformations common to all instructions
Vikram S. Advecf952cb2003-07-02 01:23:15 +0000271 visitInstruction(*curI);
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000272}
273
Misha Brukman88be7002003-10-22 03:27:45 +0000274void PreSelection::visitCallInst(CallInst &I) {
Vikram S. Adveba6f8e22003-05-31 07:34:57 +0000275 // Tell visitOperands to ignore the function name if this is a direct call.
276 visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0));
277}
278
Misha Brukman426275b2003-12-17 22:06:08 +0000279/// createPreSelectionPass - Public entry point for the PreSelection pass
280///
Chris Lattnerdfcf8e32004-04-04 20:44:05 +0000281FunctionPass* llvm::createPreSelectionPass(const TargetMachine &TM) {
Misha Brukman81c748c2003-11-07 17:31:22 +0000282 return new PreSelection(TM);
Vikram S. Advee9ac29b2002-09-20 00:29:28 +0000283}