2004-08-22 05:39:24 +08:00
|
|
|
//===- LowerPacked.cpp - Implementation of LowerPacked Transform ---------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-08-22 05:39:24 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Brad Jones and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-08-22 05:39:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements lowering Packed datatypes into more primitive
|
|
|
|
// Packed datatypes, and finally to scalar operations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-20 00:49:34 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-08-22 05:39:24 +08:00
|
|
|
#include "llvm/Argument.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Pass.h"
|
2007-02-06 07:32:05 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-08-22 05:39:24 +08:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
2006-11-26 17:46:52 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2007-10-30 04:24:00 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-08-22 05:39:24 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2005-12-26 21:48:44 +08:00
|
|
|
#include <functional>
|
2004-08-22 05:39:24 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This pass converts packed operators to an
|
|
|
|
/// equivalent operations on smaller packed data, to possibly
|
|
|
|
/// scalar operations. Currently it supports lowering
|
|
|
|
/// to scalar operations.
|
|
|
|
///
|
|
|
|
/// @brief Transforms packed instructions to simpler instructions.
|
|
|
|
///
|
2007-02-06 07:32:05 +08:00
|
|
|
class VISIBILITY_HIDDEN LowerPacked
|
|
|
|
: public FunctionPass, public InstVisitor<LowerPacked> {
|
2004-08-22 05:39:24 +08:00
|
|
|
public:
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-02 05:15:47 +08:00
|
|
|
LowerPacked() : FunctionPass((intptr_t)&ID) {}
|
|
|
|
|
2005-04-22 07:48:37 +08:00
|
|
|
/// @brief Lowers packed operations to scalar operations.
|
2004-08-22 05:39:24 +08:00
|
|
|
/// @param F The fuction to process
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
|
|
|
|
/// @brief Lowers packed load instructions.
|
|
|
|
/// @param LI the load instruction to convert
|
|
|
|
void visitLoadInst(LoadInst& LI);
|
|
|
|
|
|
|
|
/// @brief Lowers packed store instructions.
|
|
|
|
/// @param SI the store instruction to convert
|
|
|
|
void visitStoreInst(StoreInst& SI);
|
|
|
|
|
|
|
|
/// @brief Lowers packed binary operations.
|
|
|
|
/// @param BO the binary operator to convert
|
|
|
|
void visitBinaryOperator(BinaryOperator& BO);
|
|
|
|
|
2006-12-23 14:05:41 +08:00
|
|
|
/// @brief Lowers packed icmp operations.
|
2007-08-06 03:35:22 +08:00
|
|
|
/// @param IC the icmp operator to convert
|
2006-12-23 14:05:41 +08:00
|
|
|
void visitICmpInst(ICmpInst& IC);
|
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
/// @brief Lowers packed select instructions.
|
|
|
|
/// @param SELI the select operator to convert
|
|
|
|
void visitSelectInst(SelectInst& SELI);
|
|
|
|
|
2006-01-11 03:05:05 +08:00
|
|
|
/// @brief Lowers packed extractelement instructions.
|
2007-08-06 03:35:22 +08:00
|
|
|
/// @param EE the extractelement operator to convert
|
2006-01-18 04:06:55 +08:00
|
|
|
void visitExtractElementInst(ExtractElementInst& EE);
|
|
|
|
|
|
|
|
/// @brief Lowers packed insertelement instructions.
|
2007-08-06 03:35:22 +08:00
|
|
|
/// @param IE the insertelement operator to convert
|
2006-01-18 04:06:55 +08:00
|
|
|
void visitInsertElementInst(InsertElementInst& IE);
|
2006-01-11 03:05:05 +08:00
|
|
|
|
2007-10-30 04:24:00 +08:00
|
|
|
/// This function asserts that the given instruction does not have
|
|
|
|
/// vector type. Instructions with vector type should be handled by
|
|
|
|
/// the other functions in this class.
|
2005-04-22 07:48:37 +08:00
|
|
|
///
|
2007-02-15 10:26:10 +08:00
|
|
|
/// @brief Asserts if VectorType instruction is not handled elsewhere.
|
2004-08-22 05:39:24 +08:00
|
|
|
/// @param I the unhandled instruction
|
2006-11-26 17:46:52 +08:00
|
|
|
void visitInstruction(Instruction &I) {
|
2007-10-30 04:14:29 +08:00
|
|
|
assert(!isa<VectorType>(I.getType()) &&
|
|
|
|
"Unhandled Instruction with Packed ReturnType!");
|
2004-08-22 05:39:24 +08:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
/// @brief Retrieves lowered values for a packed value.
|
|
|
|
/// @param val the packed value
|
|
|
|
/// @return the lowered values
|
|
|
|
std::vector<Value*>& getValues(Value* val);
|
|
|
|
|
|
|
|
/// @brief Sets lowered values for a packed value.
|
|
|
|
/// @param val the packed value
|
|
|
|
/// @param values the corresponding lowered values
|
|
|
|
void setValues(Value* val,const std::vector<Value*>& values);
|
|
|
|
|
|
|
|
// Data Members
|
2005-04-22 07:48:37 +08:00
|
|
|
/// @brief whether we changed the function or not
|
2004-08-22 05:39:24 +08:00
|
|
|
bool Changed;
|
|
|
|
|
|
|
|
/// @brief a map from old packed values to new smaller packed values
|
|
|
|
std::map<Value*,std::vector<Value*> > packedToScalarMap;
|
|
|
|
|
|
|
|
/// Instructions in the source program to get rid of
|
|
|
|
/// after we do a pass (the old packed instructions)
|
|
|
|
std::vector<Instruction*> instrsToRemove;
|
2005-04-22 07:48:37 +08:00
|
|
|
};
|
2004-08-22 05:39:24 +08:00
|
|
|
|
2007-05-03 09:11:54 +08:00
|
|
|
char LowerPacked::ID = 0;
|
2006-08-28 06:42:52 +08:00
|
|
|
RegisterPass<LowerPacked>
|
2005-04-22 07:48:37 +08:00
|
|
|
X("lower-packed",
|
2004-08-22 05:39:24 +08:00
|
|
|
"lowers packed operations to operations on smaller packed datatypes");
|
|
|
|
|
2005-04-22 07:48:37 +08:00
|
|
|
} // end namespace
|
2004-08-22 05:39:24 +08:00
|
|
|
|
2004-11-20 00:49:34 +08:00
|
|
|
FunctionPass *llvm::createLowerPackedPass() { return new LowerPacked(); }
|
2004-11-19 01:24:20 +08:00
|
|
|
|
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
// This function sets lowered values for a corresponding
|
|
|
|
// packed value. Note, in the case of a forward reference
|
2005-04-22 07:48:37 +08:00
|
|
|
// getValues(Value*) will have already been called for
|
|
|
|
// the packed parameter. This function will then replace
|
|
|
|
// all references in the in the function of the "dummy"
|
|
|
|
// value the previous getValues(Value*) call
|
2004-08-22 05:39:24 +08:00
|
|
|
// returned with actual references.
|
|
|
|
void LowerPacked::setValues(Value* value,const std::vector<Value*>& values)
|
|
|
|
{
|
2005-04-22 07:48:37 +08:00
|
|
|
std::map<Value*,std::vector<Value*> >::iterator it =
|
2004-08-22 05:39:24 +08:00
|
|
|
packedToScalarMap.lower_bound(value);
|
|
|
|
if (it == packedToScalarMap.end() || it->first != value) {
|
|
|
|
// there was not a forward reference to this element
|
|
|
|
packedToScalarMap.insert(it,std::make_pair(value,values));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// replace forward declarations with actual definitions
|
2005-04-22 07:48:37 +08:00
|
|
|
assert(it->second.size() == values.size() &&
|
2004-08-22 05:39:24 +08:00
|
|
|
"Error forward refences and actual definition differ in size");
|
|
|
|
for (unsigned i = 0, e = values.size(); i != e; ++i) {
|
|
|
|
// replace and get rid of old forward references
|
|
|
|
it->second[i]->replaceAllUsesWith(values[i]);
|
|
|
|
delete it->second[i];
|
|
|
|
it->second[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function will examine the packed value parameter
|
|
|
|
// and if it is a packed constant or a forward reference
|
|
|
|
// properly create the lowered values needed. Otherwise
|
2005-04-22 07:48:37 +08:00
|
|
|
// it will simply retreive values from a
|
|
|
|
// setValues(Value*,const std::vector<Value*>&)
|
2004-08-22 05:39:24 +08:00
|
|
|
// call. Failing both of these cases, it will abort
|
|
|
|
// the program.
|
|
|
|
std::vector<Value*>& LowerPacked::getValues(Value* value)
|
|
|
|
{
|
2007-02-15 10:26:10 +08:00
|
|
|
assert(isa<VectorType>(value->getType()) &&
|
|
|
|
"Value must be VectorType");
|
2004-08-22 05:39:24 +08:00
|
|
|
|
|
|
|
// reject further processing if this one has
|
|
|
|
// already been handled
|
2005-04-22 07:48:37 +08:00
|
|
|
std::map<Value*,std::vector<Value*> >::iterator it =
|
2004-08-22 05:39:24 +08:00
|
|
|
packedToScalarMap.lower_bound(value);
|
|
|
|
if (it != packedToScalarMap.end() && it->first == value) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2007-02-15 10:26:10 +08:00
|
|
|
if (ConstantVector* CP = dyn_cast<ConstantVector>(value)) {
|
2004-08-22 05:39:24 +08:00
|
|
|
// non-zero constant case
|
|
|
|
std::vector<Value*> results;
|
|
|
|
results.reserve(CP->getNumOperands());
|
|
|
|
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
|
|
|
|
results.push_back(CP->getOperand(i));
|
|
|
|
}
|
|
|
|
return packedToScalarMap.insert(it,
|
|
|
|
std::make_pair(value,results))->second;
|
|
|
|
}
|
|
|
|
else if (ConstantAggregateZero* CAZ =
|
|
|
|
dyn_cast<ConstantAggregateZero>(value)) {
|
2005-04-22 07:48:37 +08:00
|
|
|
// zero constant
|
2007-02-15 10:26:10 +08:00
|
|
|
const VectorType* PKT = cast<VectorType>(CAZ->getType());
|
2004-08-22 05:39:24 +08:00
|
|
|
std::vector<Value*> results;
|
|
|
|
results.reserve(PKT->getNumElements());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
Constant* C = Constant::getNullValue(PKT->getElementType());
|
|
|
|
for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) {
|
|
|
|
results.push_back(C);
|
|
|
|
}
|
|
|
|
return packedToScalarMap.insert(it,
|
|
|
|
std::make_pair(value,results))->second;
|
|
|
|
}
|
|
|
|
else if (isa<Instruction>(value)) {
|
|
|
|
// foward reference
|
2007-02-15 10:26:10 +08:00
|
|
|
const VectorType* PKT = cast<VectorType>(value->getType());
|
2004-08-22 05:39:24 +08:00
|
|
|
std::vector<Value*> results;
|
|
|
|
results.reserve(PKT->getNumElements());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) {
|
|
|
|
results.push_back(new Argument(PKT->getElementType()));
|
|
|
|
}
|
|
|
|
return packedToScalarMap.insert(it,
|
|
|
|
std::make_pair(value,results))->second;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we don't know what it is, and we are trying to retrieve
|
|
|
|
// a value for it
|
2007-02-15 10:26:10 +08:00
|
|
|
assert(false && "Unhandled VectorType value");
|
2004-08-22 05:39:24 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LowerPacked::visitLoadInst(LoadInst& LI)
|
|
|
|
{
|
2007-02-15 11:39:18 +08:00
|
|
|
// Make sure what we are dealing with is a vector type
|
2007-02-15 10:26:10 +08:00
|
|
|
if (const VectorType* PKT = dyn_cast<VectorType>(LI.getType())) {
|
2004-08-22 05:39:24 +08:00
|
|
|
// Initialization, Idx is needed for getelementptr needed later
|
2007-10-30 04:24:00 +08:00
|
|
|
Value *Idx[2];
|
2007-10-30 04:34:35 +08:00
|
|
|
Idx[0] = ConstantInt::get(Type::Int32Ty, 0);
|
2004-08-22 05:39:24 +08:00
|
|
|
|
|
|
|
// Convert this load into num elements number of loads
|
|
|
|
std::vector<Value*> values;
|
|
|
|
values.reserve(PKT->getNumElements());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) {
|
|
|
|
// Calculate the second index we will need
|
2006-12-31 13:48:39 +08:00
|
|
|
Idx[1] = ConstantInt::get(Type::Int32Ty,i);
|
2004-08-22 05:39:24 +08:00
|
|
|
|
|
|
|
// Get the pointer
|
2007-10-30 04:34:35 +08:00
|
|
|
Value* val = new GetElementPtrInst(LI.getPointerOperand(),
|
2007-10-30 04:24:00 +08:00
|
|
|
Idx, array_endof(Idx),
|
2005-04-22 07:48:37 +08:00
|
|
|
LI.getName() +
|
2004-08-22 05:39:24 +08:00
|
|
|
".ge." + utostr(i),
|
|
|
|
&LI);
|
|
|
|
|
|
|
|
// generate the new load and save the result in packedToScalar map
|
2006-11-27 09:05:10 +08:00
|
|
|
values.push_back(new LoadInst(val, LI.getName()+"."+utostr(i),
|
|
|
|
LI.isVolatile(), &LI));
|
2004-08-22 05:39:24 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
setValues(&LI,values);
|
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&LI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LowerPacked::visitBinaryOperator(BinaryOperator& BO)
|
|
|
|
{
|
2007-02-15 10:26:10 +08:00
|
|
|
// Make sure both operands are VectorTypes
|
|
|
|
if (isa<VectorType>(BO.getOperand(0)->getType())) {
|
2004-08-22 05:39:24 +08:00
|
|
|
std::vector<Value*>& op0Vals = getValues(BO.getOperand(0));
|
|
|
|
std::vector<Value*>& op1Vals = getValues(BO.getOperand(1));
|
|
|
|
std::vector<Value*> result;
|
|
|
|
assert((op0Vals.size() == op1Vals.size()) &&
|
|
|
|
"The two packed operand to scalar maps must be equal in size.");
|
|
|
|
|
|
|
|
result.reserve(op0Vals.size());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
// generate the new binary op and save the result
|
|
|
|
for (unsigned i = 0; i != op0Vals.size(); ++i) {
|
2005-04-22 07:48:37 +08:00
|
|
|
result.push_back(BinaryOperator::create(BO.getOpcode(),
|
|
|
|
op0Vals[i],
|
2004-08-22 05:39:24 +08:00
|
|
|
op1Vals[i],
|
2005-04-22 07:48:37 +08:00
|
|
|
BO.getName() +
|
2004-08-22 05:39:24 +08:00
|
|
|
"." + utostr(i),
|
|
|
|
&BO));
|
|
|
|
}
|
|
|
|
|
|
|
|
setValues(&BO,result);
|
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&BO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 14:05:41 +08:00
|
|
|
void LowerPacked::visitICmpInst(ICmpInst& IC)
|
|
|
|
{
|
2007-02-15 10:26:10 +08:00
|
|
|
// Make sure both operands are VectorTypes
|
|
|
|
if (isa<VectorType>(IC.getOperand(0)->getType())) {
|
2006-12-23 14:05:41 +08:00
|
|
|
std::vector<Value*>& op0Vals = getValues(IC.getOperand(0));
|
|
|
|
std::vector<Value*>& op1Vals = getValues(IC.getOperand(1));
|
|
|
|
std::vector<Value*> result;
|
|
|
|
assert((op0Vals.size() == op1Vals.size()) &&
|
|
|
|
"The two packed operand to scalar maps must be equal in size.");
|
|
|
|
|
|
|
|
result.reserve(op0Vals.size());
|
|
|
|
|
|
|
|
// generate the new binary op and save the result
|
|
|
|
for (unsigned i = 0; i != op0Vals.size(); ++i) {
|
|
|
|
result.push_back(CmpInst::create(IC.getOpcode(),
|
|
|
|
IC.getPredicate(),
|
|
|
|
op0Vals[i],
|
|
|
|
op1Vals[i],
|
|
|
|
IC.getName() +
|
|
|
|
"." + utostr(i),
|
|
|
|
&IC));
|
|
|
|
}
|
|
|
|
|
|
|
|
setValues(&IC,result);
|
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&IC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
void LowerPacked::visitStoreInst(StoreInst& SI)
|
|
|
|
{
|
2007-02-15 10:26:10 +08:00
|
|
|
if (const VectorType* PKT =
|
|
|
|
dyn_cast<VectorType>(SI.getOperand(0)->getType())) {
|
2004-08-22 05:39:24 +08:00
|
|
|
// We will need this for getelementptr
|
2007-10-30 04:24:00 +08:00
|
|
|
Value *Idx[2];
|
2007-10-30 04:34:35 +08:00
|
|
|
Idx[0] = ConstantInt::get(Type::Int32Ty, 0);
|
2006-11-27 09:05:10 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
std::vector<Value*>& values = getValues(SI.getOperand(0));
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
assert((values.size() == PKT->getNumElements()) &&
|
2007-02-15 11:11:20 +08:00
|
|
|
"Scalar must have the same number of elements as Vector Type");
|
2004-08-22 05:39:24 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) {
|
|
|
|
// Generate the indices for getelementptr
|
2006-12-31 13:48:39 +08:00
|
|
|
Idx[1] = ConstantInt::get(Type::Int32Ty,i);
|
2007-10-30 04:34:35 +08:00
|
|
|
Value* val = new GetElementPtrInst(SI.getPointerOperand(),
|
2007-10-30 04:24:00 +08:00
|
|
|
Idx, array_endof(Idx),
|
2004-08-22 05:39:24 +08:00
|
|
|
"store.ge." +
|
|
|
|
utostr(i) + ".",
|
|
|
|
&SI);
|
|
|
|
new StoreInst(values[i], val, SI.isVolatile(),&SI);
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&SI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LowerPacked::visitSelectInst(SelectInst& SELI)
|
|
|
|
{
|
2007-02-15 10:26:10 +08:00
|
|
|
// Make sure both operands are VectorTypes
|
|
|
|
if (isa<VectorType>(SELI.getType())) {
|
2004-08-22 05:39:24 +08:00
|
|
|
std::vector<Value*>& op0Vals = getValues(SELI.getTrueValue());
|
|
|
|
std::vector<Value*>& op1Vals = getValues(SELI.getFalseValue());
|
|
|
|
std::vector<Value*> result;
|
|
|
|
|
|
|
|
assert((op0Vals.size() == op1Vals.size()) &&
|
|
|
|
"The two packed operand to scalar maps must be equal in size.");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != op0Vals.size(); ++i) {
|
|
|
|
result.push_back(new SelectInst(SELI.getCondition(),
|
2005-04-22 07:48:37 +08:00
|
|
|
op0Vals[i],
|
2004-08-22 05:39:24 +08:00
|
|
|
op1Vals[i],
|
|
|
|
SELI.getName()+ "." + utostr(i),
|
|
|
|
&SELI));
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
setValues(&SELI,result);
|
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&SELI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-11 03:05:05 +08:00
|
|
|
void LowerPacked::visitExtractElementInst(ExtractElementInst& EI)
|
|
|
|
{
|
|
|
|
std::vector<Value*>& op0Vals = getValues(EI.getOperand(0));
|
2007-02-15 10:26:10 +08:00
|
|
|
const VectorType *PTy = cast<VectorType>(EI.getOperand(0)->getType());
|
2006-01-11 03:05:05 +08:00
|
|
|
Value *op1 = EI.getOperand(1);
|
|
|
|
|
2006-10-20 15:07:24 +08:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(op1)) {
|
|
|
|
EI.replaceAllUsesWith(op0Vals[C->getZExtValue()]);
|
2006-01-11 03:05:05 +08:00
|
|
|
} else {
|
2006-01-18 04:06:55 +08:00
|
|
|
AllocaInst *alloca =
|
|
|
|
new AllocaInst(PTy->getElementType(),
|
2006-12-31 13:48:39 +08:00
|
|
|
ConstantInt::get(Type::Int32Ty, PTy->getNumElements()),
|
2006-01-18 04:06:55 +08:00
|
|
|
EI.getName() + ".alloca",
|
2007-04-17 02:10:23 +08:00
|
|
|
EI.getParent()->getParent()->getEntryBlock().begin());
|
2006-01-11 03:05:05 +08:00
|
|
|
for (unsigned i = 0; i < PTy->getNumElements(); ++i) {
|
2006-01-18 04:06:55 +08:00
|
|
|
GetElementPtrInst *GEP =
|
2006-12-31 13:48:39 +08:00
|
|
|
new GetElementPtrInst(alloca, ConstantInt::get(Type::Int32Ty, i),
|
2006-01-18 04:06:55 +08:00
|
|
|
"store.ge", &EI);
|
2006-01-11 03:05:05 +08:00
|
|
|
new StoreInst(op0Vals[i], GEP, &EI);
|
|
|
|
}
|
2006-01-18 04:06:55 +08:00
|
|
|
GetElementPtrInst *GEP =
|
|
|
|
new GetElementPtrInst(alloca, op1, EI.getName() + ".ge", &EI);
|
2006-01-11 03:05:05 +08:00
|
|
|
LoadInst *load = new LoadInst(GEP, EI.getName() + ".load", &EI);
|
|
|
|
EI.replaceAllUsesWith(load);
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&EI);
|
|
|
|
}
|
|
|
|
|
2006-01-18 04:06:55 +08:00
|
|
|
void LowerPacked::visitInsertElementInst(InsertElementInst& IE)
|
|
|
|
{
|
|
|
|
std::vector<Value*>& Vals = getValues(IE.getOperand(0));
|
|
|
|
Value *Elt = IE.getOperand(1);
|
|
|
|
Value *Idx = IE.getOperand(2);
|
|
|
|
std::vector<Value*> result;
|
|
|
|
result.reserve(Vals.size());
|
|
|
|
|
2006-10-20 15:07:24 +08:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(Idx)) {
|
|
|
|
unsigned idxVal = C->getZExtValue();
|
2006-01-18 04:06:55 +08:00
|
|
|
for (unsigned i = 0; i != Vals.size(); ++i) {
|
|
|
|
result.push_back(i == idxVal ? Elt : Vals[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i != Vals.size(); ++i) {
|
2006-12-23 14:05:41 +08:00
|
|
|
ICmpInst *icmp =
|
|
|
|
new ICmpInst(ICmpInst::ICMP_EQ, Idx,
|
2006-12-31 13:48:39 +08:00
|
|
|
ConstantInt::get(Type::Int32Ty, i),
|
2006-12-23 14:05:41 +08:00
|
|
|
"icmp", &IE);
|
2006-01-18 04:06:55 +08:00
|
|
|
SelectInst *select =
|
2006-12-23 14:05:41 +08:00
|
|
|
new SelectInst(icmp, Elt, Vals[i], "select", &IE);
|
2006-01-18 04:06:55 +08:00
|
|
|
result.push_back(select);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setValues(&IE, result);
|
|
|
|
Changed = true;
|
|
|
|
instrsToRemove.push_back(&IE);
|
|
|
|
}
|
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
bool LowerPacked::runOnFunction(Function& F)
|
|
|
|
{
|
|
|
|
// initialize
|
2005-04-22 07:48:37 +08:00
|
|
|
Changed = false;
|
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
// Does three passes:
|
2005-04-22 07:48:37 +08:00
|
|
|
// Pass 1) Converts Packed Operations to
|
2004-08-22 05:39:24 +08:00
|
|
|
// new Packed Operations on smaller
|
|
|
|
// datatypes
|
|
|
|
visit(F);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-08-22 05:39:24 +08:00
|
|
|
// Pass 2) Drop all references
|
|
|
|
std::for_each(instrsToRemove.begin(),
|
|
|
|
instrsToRemove.end(),
|
|
|
|
std::mem_fun(&Instruction::dropAllReferences));
|
|
|
|
|
|
|
|
// Pass 3) Delete the Instructions to remove aka packed instructions
|
2005-04-22 07:48:37 +08:00
|
|
|
for (std::vector<Instruction*>::iterator i = instrsToRemove.begin(),
|
|
|
|
e = instrsToRemove.end();
|
2004-08-22 05:39:24 +08:00
|
|
|
i != e; ++i) {
|
2005-04-22 07:48:37 +08:00
|
|
|
(*i)->getParent()->getInstList().erase(*i);
|
2004-08-22 05:39:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// clean-up
|
|
|
|
packedToScalarMap.clear();
|
|
|
|
instrsToRemove.clear();
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|