LLVM API Documentation

ValueMapper.cpp

Go to the documentation of this file.
00001 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MapValue function, which is shared by various parts of
00011 // the lib/Transforms/Utils library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "ValueMapper.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/GlobalValue.h"
00018 #include "llvm/Instruction.h"
00019 using namespace llvm;
00020 
00021 Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
00022   Value *&VMSlot = VM[V];
00023   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
00024 
00025   // Global values do not need to be seeded into the ValueMap if they are using
00026   // the identity mapping.
00027   if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
00028     return VMSlot = const_cast<Value*>(V);
00029 
00030   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
00031     if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
00032         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
00033         isa<UndefValue>(C) || isa<InlineAsm>(V))
00034       return VMSlot = C;           // Primitive constants map directly
00035     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
00036       for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
00037         Value *MV = MapValue(CA->getOperand(i), VM);
00038         if (MV != CA->getOperand(i)) {
00039           // This array must contain a reference to a global, make a new array
00040           // and return it.
00041           //
00042           std::vector<Constant*> Values;
00043           Values.reserve(CA->getNumOperands());
00044           for (unsigned j = 0; j != i; ++j)
00045             Values.push_back(CA->getOperand(j));
00046           Values.push_back(cast<Constant>(MV));
00047           for (++i; i != e; ++i)
00048             Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
00049           return VMSlot = ConstantArray::get(CA->getType(), Values);
00050         }
00051       }
00052       return VMSlot = C;
00053 
00054     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
00055       for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
00056         Value *MV = MapValue(CS->getOperand(i), VM);
00057         if (MV != CS->getOperand(i)) {
00058           // This struct must contain a reference to a global, make a new struct
00059           // and return it.
00060           //
00061           std::vector<Constant*> Values;
00062           Values.reserve(CS->getNumOperands());
00063           for (unsigned j = 0; j != i; ++j)
00064             Values.push_back(CS->getOperand(j));
00065           Values.push_back(cast<Constant>(MV));
00066           for (++i; i != e; ++i)
00067             Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
00068           return VMSlot = ConstantStruct::get(CS->getType(), Values);
00069         }
00070       }
00071       return VMSlot = C;
00072 
00073     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00074       if (CE->getOpcode() == Instruction::Cast) {
00075         Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
00076         return VMSlot = ConstantExpr::getCast(MV, CE->getType());
00077       } else if (CE->getOpcode() == Instruction::GetElementPtr) {
00078         std::vector<Constant*> Idx;
00079         Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
00080         for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
00081           Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
00082         return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
00083       } else if (CE->getOpcode() == Instruction::Select) {
00084         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
00085         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
00086         Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
00087         return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3);
00088       } else if (CE->getOpcode() == Instruction::InsertElement) {
00089         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
00090         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
00091         Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
00092         return VMSlot = ConstantExpr::getInsertElement(MV1, MV2, MV3);
00093       } else if (CE->getOpcode() == Instruction::ExtractElement) {
00094         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
00095         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
00096         return VMSlot = ConstantExpr::getExtractElement(MV1, MV2);
00097       } else if (CE->getOpcode() == Instruction::ShuffleVector) {
00098         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
00099         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
00100         Constant *MV3 = cast<Constant>(CE->getOperand(2));
00101         return VMSlot = ConstantExpr::getShuffleVector(MV1, MV2, MV3);
00102       } else {
00103         assert(CE->getNumOperands() == 2 && "Must be binary operator?");
00104         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
00105         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
00106         return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
00107       }
00108 
00109     } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
00110       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
00111         Value *MV = MapValue(CP->getOperand(i), VM);
00112         if (MV != CP->getOperand(i)) {
00113           // This packed value must contain a reference to a global, make a new
00114           // packed constant and return it.
00115           //
00116           std::vector<Constant*> Values;
00117           Values.reserve(CP->getNumOperands());
00118           for (unsigned j = 0; j != i; ++j)
00119             Values.push_back(CP->getOperand(j));
00120           Values.push_back(cast<Constant>(MV));
00121           for (++i; i != e; ++i)
00122             Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
00123           return VMSlot = ConstantPacked::get(Values);
00124         }
00125       }
00126       return VMSlot = C;
00127       
00128     } else {
00129       assert(0 && "Unknown type of constant!");
00130     }
00131   }
00132 
00133   V->dump();
00134   assert(0 && "Unknown value type: why didn't it get resolved?!");
00135   return 0;
00136 }
00137 
00138 /// RemapInstruction - Convert the instruction operands from referencing the
00139 /// current values into those specified by ValueMap.
00140 ///
00141 void llvm::RemapInstruction(Instruction *I,
00142                             std::map<const Value *, Value*> &ValueMap) {
00143   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
00144     const Value *Op = I->getOperand(op);
00145     Value *V = MapValue(Op, ValueMap);
00146     assert(V && "Referenced value not in value map!");
00147     I->setOperand(op, V);
00148   }
00149 }