blob: d7507f9f07fb29ae30b7241ab2f8e883fb2db289 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
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 Lattner51cbcbf2002-11-20 20:47:41 +00009//
10// This file defines the MapValue function, which is shared by various parts of
11// the lib/Transforms/Utils library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ValueMapper.h"
16#include "llvm/Constants.h"
Reid Spencer518310c2004-07-18 00:44:37 +000017#include "llvm/GlobalValue.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000018#include "llvm/Instruction.h"
Reid Spencer954da372004-07-04 12:19:56 +000019#include <iostream>
20
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022
Chris Lattnerf7703df2004-01-09 06:12:26 +000023Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000024 Value *&VMSlot = VM[V];
25 if (VMSlot) return VMSlot; // Does it exist in the map yet?
26
Chris Lattner5f92e2b2003-10-06 15:23:43 +000027 // Global values do not need to be seeded into the ValueMap if they are using
28 // the identity mapping.
29 if (isa<GlobalValue>(V))
30 return VMSlot = const_cast<Value*>(V);
31
Chris Lattner15faa842003-04-18 03:49:49 +000032 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000033 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
Chris Lattnerde512b52004-02-15 05:55:15 +000034 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000035 return VMSlot = C; // Primitive constants map directly
Reid Spencere95ff9a2004-07-18 08:41:47 +000036 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000037 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
38 Value *MV = MapValue(CA->getOperand(i), VM);
39 if (MV != CA->getOperand(i)) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000040 // This array must contain a reference to a global, make a new array
41 // and return it.
42 //
43 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000044 Values.reserve(CA->getNumOperands());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000045 for (unsigned j = 0; j != i; ++j)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000046 Values.push_back(CA->getOperand(j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000047 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000048 for (++i; i != e; ++i)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000049 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000050 return VMSlot = ConstantArray::get(CA->getType(), Values);
51 }
52 }
53 return VMSlot = C;
54
55 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000056 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
57 Value *MV = MapValue(CS->getOperand(i), VM);
58 if (MV != CS->getOperand(i)) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000059 // This struct must contain a reference to a global, make a new struct
60 // and return it.
61 //
62 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000063 Values.reserve(CS->getNumOperands());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000064 for (unsigned j = 0; j != i; ++j)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000065 Values.push_back(CS->getOperand(j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000066 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000067 for (++i; i != e; ++i)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000068 Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000069 return VMSlot = ConstantStruct::get(CS->getType(), Values);
70 }
71 }
72 return VMSlot = C;
73
74 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
75 if (CE->getOpcode() == Instruction::Cast) {
76 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
77 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
78 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
79 std::vector<Constant*> Idx;
80 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
81 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
82 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
83 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
Chris Lattnereb6f18f2004-08-13 02:43:19 +000084 } else if (CE->getOpcode() == Instruction::Select) {
85 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
86 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
87 Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
88 return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000089 } else {
90 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
91 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
92 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
93 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
94 }
95
96 } else {
97 assert(0 && "Unknown type of constant!");
98 }
99 }
Chris Lattner3cf5db72003-01-13 00:52:25 +0000100
101 V->dump();
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000102 assert(0 && "Unknown value type: why didn't it get resolved?!");
103 return 0;
104}
Brian Gaeke6129af32004-05-19 09:08:12 +0000105
106/// RemapInstruction - Convert the instruction operands from referencing the
107/// current values into those specified by ValueMap.
108///
109void llvm::RemapInstruction(Instruction *I,
110 std::map<const Value *, Value*> &ValueMap) {
111 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
112 const Value *Op = I->getOperand(op);
113 Value *V = MapValue(Op, ValueMap);
114#ifndef NDEBUG
115 if (!V) {
Chris Lattner30b43442004-07-15 02:06:12 +0000116 std::cerr << "Val = \n" << *Op << "Addr = " << (void*)Op;
117 std::cerr << "\nInst = " << *I;
Brian Gaeke6129af32004-05-19 09:08:12 +0000118 }
119#endif
120 assert(V && "Referenced value not in value map!");
121 I->setOperand(op, V);
122 }
123}