blob: 6db9ed55de295d0e20fc4d09861df814b3eec137 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Chris Lattnerf7703df2004-01-09 06:12:26 +000019using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000020
Chris Lattnerf7703df2004-01-09 06:12:26 +000021Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022 Value *&VMSlot = VM[V];
23 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Misha Brukmanfd939082005-04-21 23:48:37 +000024
Chris Lattner5f92e2b2003-10-06 15:23:43 +000025 // Global values do not need to be seeded into the ValueMap if they are using
26 // the identity mapping.
27 if (isa<GlobalValue>(V))
28 return VMSlot = const_cast<Value*>(V);
29
Chris Lattner15faa842003-04-18 03:49:49 +000030 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000031 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
Chris Lattner82731c72004-10-16 18:10:31 +000032 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Chris Lattnerf47a6b42006-01-26 01:55:22 +000033 isa<UndefValue>(C) || isa<InlineAsm>(V))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000034 return VMSlot = C; // Primitive constants map directly
Reid Spencere95ff9a2004-07-18 08:41:47 +000035 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000036 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
37 Value *MV = MapValue(CA->getOperand(i), VM);
38 if (MV != CA->getOperand(i)) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000039 // This array must contain a reference to a global, make a new array
40 // and return it.
41 //
42 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000043 Values.reserve(CA->getNumOperands());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000044 for (unsigned j = 0; j != i; ++j)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000045 Values.push_back(CA->getOperand(j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000046 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000047 for (++i; i != e; ++i)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000048 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000049 return VMSlot = ConstantArray::get(CA->getType(), Values);
50 }
51 }
52 return VMSlot = C;
53
54 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000055 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
56 Value *MV = MapValue(CS->getOperand(i), VM);
57 if (MV != CS->getOperand(i)) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000058 // This struct must contain a reference to a global, make a new struct
59 // and return it.
60 //
61 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000062 Values.reserve(CS->getNumOperands());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000063 for (unsigned j = 0; j != i; ++j)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000064 Values.push_back(CS->getOperand(j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000065 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000066 for (++i; i != e; ++i)
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000067 Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000068 return VMSlot = ConstantStruct::get(CS->getType(), Values);
69 }
70 }
71 return VMSlot = C;
72
73 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
74 if (CE->getOpcode() == Instruction::Cast) {
75 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
76 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
77 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
78 std::vector<Constant*> Idx;
79 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
80 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
81 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
82 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
Chris Lattnereb6f18f2004-08-13 02:43:19 +000083 } else if (CE->getOpcode() == Instruction::Select) {
84 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
85 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
86 Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
87 return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000088 } else {
89 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
90 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
91 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
92 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
93 }
94
Chris Lattner49aaa6a2006-03-27 05:50:18 +000095 } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
96 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
97 Value *MV = MapValue(CP->getOperand(i), VM);
98 if (MV != CP->getOperand(i)) {
99 // This packed value must contain a reference to a global, make a new
100 // packed constant and return it.
101 //
102 std::vector<Constant*> Values;
103 Values.reserve(CP->getNumOperands());
104 for (unsigned j = 0; j != i; ++j)
105 Values.push_back(CP->getOperand(j));
106 Values.push_back(cast<Constant>(MV));
107 for (++i; i != e; ++i)
108 Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
109 return VMSlot = ConstantPacked::get(Values);
110 }
111 }
112 return VMSlot = C;
113
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000114 } else {
115 assert(0 && "Unknown type of constant!");
116 }
117 }
Chris Lattner3cf5db72003-01-13 00:52:25 +0000118
119 V->dump();
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000120 assert(0 && "Unknown value type: why didn't it get resolved?!");
121 return 0;
122}
Brian Gaeke6129af32004-05-19 09:08:12 +0000123
124/// RemapInstruction - Convert the instruction operands from referencing the
125/// current values into those specified by ValueMap.
126///
127void llvm::RemapInstruction(Instruction *I,
128 std::map<const Value *, Value*> &ValueMap) {
129 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
130 const Value *Op = I->getOperand(op);
131 Value *V = MapValue(Op, ValueMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000132 assert(V && "Referenced value not in value map!");
133 I->setOperand(op, V);
134 }
135}